Reputation: 365
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!
Upvotes: 1
Views: 2600
Reputation: 3158
The reason you're only getting the visible rows is because cells are released when you scroll them offscreen. Instead of your current solution, I'd recommend overriding the -tableView:didSelectRowAtIndexPath:
method. In this method, test whether the cell at the index path passed into the method has a checkmark, and if so, store the index path in an NSMutableSet
accordingly.
Example:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[tableView cellForRowAtIndexPath:indexPath] accessoryType] == UITableViewCellAccessoryCheckmark) {
[_selectedCellIndexes addObject:indexPath];
}
}
Upvotes: 4