1

I have checked some rows programatically at cellForRowAtIndexPath, and it is showing and working fine. But now I need to fetch the check marked rows. I use the following code:

for (int i=0; i<10; i++) {

    NSIndexPath *index = [NSIndexPath indexPathForRow:i inSection:0];

    if([motorwayTable cellForRowAtIndexPath:index].accessoryType == UITableViewCellAccessoryCheckmark)
    {
        NSLog(@"Selected Rows: %d",i);
    }
}

And here is my cellForRowAtIndexPath method:

- (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];
    //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}


cell.textLabel.text = [NSString stringWithFormat:@"arefin %d",[indexPath row]];

if(indexPath.row == 8 || indexPath.row == 7 || indexPath.row == 9)
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    cell.selected = YES;
}
else
{
    cell.accessoryType = UITableViewCellAccessoryNone;

}

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

return cell;

}

But the problem is I am getting only visible check marked rows, not getting that are not visible yet. How I can get check marked rows that are not visible yet in UITableView but checked programatically in cellForRowAtIndexPath.

Thanks in Advance!

4

1 回答 1

4

您仅获得可见行的原因是因为当您将它们滚动到屏幕外时会释放单元格。我建议您覆盖该-tableView:didSelectRowAtIndexPath:方法,而不是您当前的解决方案。在该方法中,测试传入该方法的索引路径处的单元格是否有勾选标记,如果有,则将索引路径存储在相应的an中NSMutableSet

例子:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if ([[tableView cellForRowAtIndexPath:indexPath] accessoryType] == UITableViewCellAccessoryCheckmark) {
            [_selectedCellIndexes addObject:indexPath];
        }
}
于 2013-05-11T18:04:31.230 回答