Search code examples
objective-ciosxcodeuitableviewdidselectrowatindexpath

Checking/Unchecking a tableviewcell with sections


Previously, I had a large-ish dataset (~530 records) being displayed in a tableview. The data was held in an array of dictionaries with two keys, ID and name. Previously, I tapped a cell, it added a check mark, and it sent the cell row number to a 'selectedArray'. As I had already alphabetically sorted them (still in one section), the indexPath.row which was stored in the selectedArray corresponded to the dictionaries' array index, so I could pull data (the ID) from the record.

I decided that I would split this into headers by alphabetical order (which was an absolute pain, I don't see why it's such a complex process to insert headers into a list of records). But now, as I only used indexPath.row, when I tick the first one, it ticks the first record of each section, and only returns the number 0, so I only get the first record in the whole dataset. Is there a simple way to correct this? Really appreciate any help.

cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"inviteCell"];

//    cell.accessoryType = UITableViewCellAccessoryCheckmark;

    if ([checkedCells objectForKey:[NSNumber numberWithInt:indexPath.row]] != nil) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }


//    NSDictionary *friend = [sortedFriendsArray objectAtIndex:indexPath.row];


    //---get the letter in the current section---
    NSString *alphabet = [nameIndex objectAtIndex:[indexPath section]];
    //---get all states beginning with the letter---
    NSPredicate *predicate =
    [NSPredicate predicateWithFormat:@"SELF.name beginswith[c] %@", alphabet];
    NSArray *states = [sortedFriendsArray filteredArrayUsingPredicate:predicate];
    if ([states count]>0) {
//---extract the relevant state from the states object---

        NSDictionary *friend = [states objectAtIndex:indexPath.row];
        long long fbid = [[friend objectForKey:@"id"]longLongValue];
        NSString *name = [friend objectForKey:@"name"];

        NSString *urlString = [NSString stringWithFormat:@"https://graph.facebook.com/%qi/picture?type=square",fbid];
        NSURL *url = [NSURL URLWithString:urlString];

        UILabel *eventNameLabel = (UILabel *) [cell viewWithTag:1];
        eventNameLabel.text = name;

        UIImageView *eventLogo = (UIImageView*) [cell viewWithTag:2];
        eventLogo.image = [UIImage imageNamed:@"112-group.png"];
        //    eventLogo.image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];;

    }

        return cell;

}

CURRENT didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSNumber *indexNumber = [NSNumber numberWithInt:indexPath.row];
    NSDictionary *friend = [sortedFriendsArray objectAtIndex:indexPath.row];
    long long fbid = [[friend objectForKey:@"id"]longLongValue];
    NSNumber *fbidNum = [NSNumber numberWithLongLong:fbid];

    if ([checkedCells objectForKey:indexNumber] != nil) {
        [checkedCells removeObjectForKey:indexNumber];
        cell.accessoryType = UITableViewCellAccessoryNone;

    }
    else
    {
        [checkedCells setObject:fbidNum forKey:indexNumber];
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }


    NSLog(@"Cell Pressed: %@",indexNumber);
    NSLog(@"FBID: %lld",fbid);
    NSLog(@"Array: %@",checkedCells);
}

Solution

  • It looks like you're only saving the checked cells by row, not by section and row. At minimum, don't you need to be testing the section as well as the row in this block of code?

    if ([checkedCells objectForKey:[NSNumber numberWithInt:indexPath.row]] != nil) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    

    It's late, and I don't have my Mac handy to try this code, so maybe I'm missing something obvious. I'm not sure what you're talking about only returning zero ... is that indexPath.row?

    EDIT:

    To account for the section in your data array, I'd suggest storing the data as a dictionary of arrays, one dictionary entry keyed by each letter, with the inner array holding all of the entries starting with that letter. It's a little more complicated to retrieve the data, but it correctly accounts for the section.

    I supposed you could create some kind of an offset to account for the number of entries in each section and then use that to index into a flat array, but that would be a lot harder to maintain, in my view. I think the dictionary of arrays is the way to go.