Ashish Patil
Ashish Patil

Reputation: 471

How to maintain state of selection of row in table view?

i am a beginner in xcode. I am creating a questionnaire type list with multiple rows in scroll. But when I select a row and scroll it doesn't retains its state and when I come back, it also loses its selection. So anyone please kindly let me know how to achieve that,i have tried something like this,

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(val==1)
    {
        checkedArr=[[NSMutableArray alloc] init];
        for (int i = 0; i<17; i++)
        {
            [checkedArr addObject:@"1"];
        }
        NSLog(@"Checked arr size %i",[checkedArr count]);

        return 17;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%i",indexPath.row];
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell==nil)
    {
        cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.textLabel.numberOfLines = 0;
        cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:14.0];
    }
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18.0];

    UIView *selectionColor = [[UIView alloc] init];
    selectionColor.backgroundColor = [UIColor colorWithRed:99/255.0f green:209/255.0f blue:248/255.0f alpha:1.0];
    cell.selectedBackgroundView = selectionColor;

    if([[checkedArr objectAtIndex:indexPath.row] isEqual:@"0"])
    {
        cell.accessoryView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tick.png"]];
        NSLog(@"checkedArr 0000000");
    }
    else if ([[checkedArr objectAtIndex:indexPath.row] isEqual:@"1"])
    {
        cell.accessoryView=nil;
        NSLog(@"checkedArr 111111");
    }


    cell.textLabel.frame=CGRectMake(75.0, 50.0, 150.0, 20.0);
    cell.textLabel.text=[listArray objectAtIndex:indexPath.row];
    NSLog(@"Checked arr size %i",[checkedArr count]);

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell=[questionTable cellForRowAtIndexPath:indexPath];
  [checkedArr replaceObjectAtIndex:indexPath.row withObject:@"0"];
    if([[checkedArr objectAtIndex:indexPath.row] isEqual:@"0"])
    {
        cell.accessoryView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tick.png"]];

        NSLog(@"checkedArr 0000000");

    }
    else if ([[checkedArr objectAtIndex:indexPath.row] isEqual:@"1"])
    {[questionTable deselectRowAtIndexPath:indexPath animated:YES];
        cell.accessoryView=nil;
        NSLog(@"checkedArr 111111");
    }

    NSLog(@"Val is %i",val);
    NSLog(@"selected is %@",[listArray objectAtIndex:indexPath.row]);
//    NSLog(@"Checked arr descripton %@",[checkedArr description]);
}

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView cellForRowAtIndexPath:indexPath].accessoryView = UITableViewCellAccessoryNone;
}

Upvotes: 0

Views: 1326

Answers (4)

CyberMew
CyberMew

Reputation: 1421

If you are using dynamic cells, and updating your row with some kind of class variable like a NSArray, you might have set your variable to weak instead of strong.

Upvotes: 0

Dev Patel
Dev Patel

Reputation: 290

In my application i used same code for check mark as accessoryType,check this.

take it in .h file mutable array arSelectedRows;

 - (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 setSelectionStyle:UITableViewCellSelectionStyleGray];

   //Do anything you want for cell here
  if([arSelectedRows containsObject:indexPath]) {
       cell.accessoryType = UITableViewCellAccessoryCheckmark;
 }
  else {
        cell.accessoryType = UITableViewCellAccessoryNone;
  }
 return cell;
}

 #pragma mark - Table view delegate

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
   if(cell.accessoryType == UITableViewCellAccessoryNone) {
       cell.accessoryType = UITableViewCellAccessoryCheckmark;
       [arSelectedRows addObject:indexPath];
   }
   else {
      cell.accessoryType = UITableViewCellAccessoryNone;
     [arSelectedRows removeObject:indexPath];
 }
 NSLog(@"arSelectedRows are  :%@",arSelectedRows);

 [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

EDIT

  //if you want only single check mark 

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {

     [arSelectedRows removeAllObjects];

      UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
     if(cell.accessoryType == UITableViewCellAccessoryNone) {
          cell.accessoryType = UITableViewCellAccessoryCheckmark;
         [arSelectedRows addObject:indexPath];
     }
      else {
        cell.accessoryType = UITableViewCellAccessoryNone;
        [arSelectedRows removeObject:indexPath];

      }

      NSLog(@"arSelectedRows are:%@",arSelectedRows);


     [self.tableview reloadData];//Reload your tableview here
 }

it will help you.

Upvotes: 2

Hussain Shabbir
Hussain Shabbir

Reputation: 14995

I think you need to use this api below:-

- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition

Upvotes: 0

iPatel
iPatel

Reputation: 47049

First your cellForRowAtIndexPath method create new cell each time whenever you scroll up/down tableView, it is very bad for memory management.

Just remove [questionTable deselectRowAtIndexPath:indexPath animated:YES] in your cellForRowAtIndexPath method

Upvotes: 0

Related Questions