0

I am implementing filters in a UITableView through checkmarks for different title headings .

According to selected cells,a query runs on local database and i get a result.I am saving filter result in an array and pass this array through push segue into another uitableviewview and display the result.

I am using modal segue to go on the filter screen.

But the problem is that when i get back to filter screen, my selected filters which are checkmarked cells disappears.

Here is my code to select rows:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    long sec = [indexPath section];
    if(sec==0){
    if(cell.accessoryType == UITableViewCellAccessoryNone) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [sportsSelectedRows addObject:indexPath];
    }
    else {
        cell.accessoryType = UITableViewCellAccessoryNone;
        [sportsSelectedRows removeObject:indexPath];
    }

    }
    else if(sec==1){
        if(cell.accessoryType == UITableViewCellAccessoryNone) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
            [areaSelectedRows addObject:indexPath];
    }
        else {
            cell.accessoryType = UITableViewCellAccessoryNone;
            [areaSelectedRows removeObject:indexPath];}
    }
else if(sec==2){
    cell.tag = indexPath.section;
    for (UITableViewCell *cell in [tableView visibleCells]) {
            if (cell.accessoryType != UITableViewCellAccessoryNone && cell.tag == indexPath.section) {
                cell.accessoryType = UITableViewCellAccessoryNone;
            }
        }
    if(cell.accessoryType == UITableViewCellAccessoryNone) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
            [daySelectedRows removeAllObjects];
            [daySelectedRows addObject:indexPath];
        }
        else {
            cell.accessoryType = UITableViewCellAccessoryNone;
            [daySelectedRows removeObject:indexPath];}
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

Could you help in solving this.

  • possible duplicate of [How do I maintain the UITableView selected row when returning from a UINavigationController child screen?](http://stackoverflow.com/questions/6795214/how-do-i-maintain-the-uitableview-selected-row-when-returning-from-a-uinavigatio) – jlehr Jan 19 '15 at 19:17
  • thanks @jlehr i will check this and get back to you. – Daljeet Singh Jan 19 '15 at 19:20

1 Answers1

0

Inside didSelectRowAtIndexPath: don't do anything except record the fact that the item/path should now be selected or not selected. Move the accessory handling into cellForRowAtIndexPath:.

Phillip Mills
  • 30,888
  • 4
  • 42
  • 57
  • It's perfectly fine to update the selected cell's accessory type in `didSelectRowAtIndexPath`. It's far more efficient than reloading that row just to toggle the checkmark. – rmaddy Jan 19 '15 at 19:04
  • I'd use `tableView:didSelectRowAtIndexPath:` for the currently selected cell and `tableView:willDisplayCell:forRowAtIndexPath:` for setting the right stuf for rows that are about to appear on the screen. – vikingosegundo Jan 19 '15 at 19:11
  • Thanks @rmaddy for quick reply, could you please give me an example or edit my code. – Daljeet Singh Jan 19 '15 at 19:15
  • thanks @vikingosegundo could you please brief me more about what you want to say. – Daljeet Singh Jan 19 '15 at 19:16