table view with objects issues

Discussion in 'iOS Programming' started by LARRYSE, Sep 29, 2012.

  1. LARRYSE, Sep 29, 2012
    Last edited by a moderator: Sep 29, 2012

    LARRYSE macrumors newbie

    Joined:
    Aug 22, 2012
    #1
    I have a tableview being controlled by a segmented controller. My table contains info from an object with 2 properties, type and advice. if segment 0 is pressed the table fills with object advice property of type 0, if it is 1 it fills with type 1. here my issue, by default 0 shows and shows only type 0 but it repeats a few of the advice rows at the bottom.

    if i press 1 it shows some type 0 then all of type 1.

    segment code:
    Code:
    -(IBAction)segmentChange:(id)sender
    {
        if (segment.selectedSegmentIndex == 0)
        {
            [self.tableView reloadData];
        }
        if (segment.selectedSegmentIndex == 1)
        {
            [self.tableView reloadData];
        }
    }
    
    table code:
    Code:
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }
        adviceDomain *ad = [adviceList objectAtIndex:indexPath.row];
           NSLog(@"segment selected: %i",segment.selectedSegmentIndex);
        // Configure the cell...
        if(segment.selectedSegmentIndex == 0)
        {
            if([ad.type isEqualToString:@"Artists"])
            {       
                cell.textLabel.text = ad.advice;
            }
        }
        if(segment.selectedSegmentIndex == 1)
        {
            if([ad.type isEqualToString:@"Producers"])
            {       
                cell.textLabel.text = ad.advice;
            }
        }    
        
        cell.textLabel.font = [UIFont systemFontOfSize:17];
        cell.textLabel.numberOfLines = 0;
        cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;    
        return cell;
    }
    
    any suggestions?
    thanks
     
  2. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    A few things:

    1 - Your closing tags have the wrong slash. They should contain /code, not \code, to close the code tags.

    2 - For your segmentChanged method, why not just have [self.tableView reloadData];? Either way that's all you do in the method... it would keep your code a bit neater.

    3 - Your code needs to be rethought in assigning the text to the cells.

    It could be written like this:

    Code:
    ...
    if (segment.selectedSegmentIndex == 0 && [ad.type isEqualToString:@"Artists"]) {
        cell.textLabel.text = ad.advice;
    } else if (segment.selectedSegmentIndex == 1 && [ad.type isEqualToString:@"Producers"]) {
        cell.textLabel.text = ad.advice;
    }
    ...
    That code makes no sense. You end up with repeating cells because the dequeue method will grab cells that have scrolled off of screen and reuse them for cells that are about to appear. If you haven't assigned the cells new text, they'll have the exact same text that they had before. This means that you're either having the selectedSegmentIndex be 0 and the ad.type be Producers or you're having the selectedSegmentIndex be 1 and the ad.type be Artists, neither of which are conditions under which your code will provide any new text for the cells.

    Without knowing more about what you're doing, I'd suggest having two separate arrays, one for your artists, and one for your producers, and swap between them based on which segment is selected.

    One other suggestion: look into using a typedef enum in place of 0 and 1 for your segment indexes. It'll help make your code easier to read:
    http://stackoverflow.com/questions/707512/what-is-a-typedef-enum-in-objective-c
     
  3. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #3
    Mod note: Fixed code tags, just report it and we can fix it.

    B
     

Share This Page