0

以前,我在表格视图中显示了一个大型数据集(约 530 条记录)。数据保存在具有两个键 ID 和名称的字典数组中。以前,我点击了一个单元格,它添加了一个复选标记,并将单元格行号发送到“selectedArray”。因为我已经按字母顺序对它们进行了排序(仍然在一个部分中),所以存储在 selectedArray 中的 indexPath.row 对应于字典的数组索引,因此我可以从记录中提取数据(ID)。

我决定按字母顺序将其拆分为标题(这绝对是一种痛苦,我不明白为什么将标题插入记录列表是一个如此复杂的过程)。但是现在,由于我只使用了indexPath.row,所以当我勾选第一个时,它会勾选每个部分的第一条记录,并且只返回数字0,所以我只获取整个数据集中的第一条记录。有没有一种简单的方法来纠正这个问题?非常感谢任何帮助。

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;

}

当前 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);
}
4

1 回答 1

0

看起来您只是按行保存选中的单元格,而不是按部分和行。至少,您不需要测试这段代码中的部分和行吗?

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

已经很晚了,我没有方便的 Mac 来尝试这段代码,所以我可能遗漏了一些明显的东西。我不确定你在说什么只返回零……那是 indexPath.row 吗?

编辑:

为了说明数据数组中的部分,我建议将数据存储为数组字典,一个字典条目由每个字母键入,内部数组包含以该字母开头的所有条目。检索数据有点复杂,但它正确地解释了该部分。

我想你可以创建某种偏移量来计算每个部分中的条目数,然后使用它来索引一个平面数组,但在我看来,这将更难维护。我认为数组字典是要走的路。

于 2012-05-28T03:28:15.587 回答