Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
445 views
in Technique[技术] by (71.8m points)

objective c - Multiple checkMark when row selected in UITableView IOS

I have a UITableView that displays checkmarks when a row is selected. The problem is that When i select a row in didSelectRowAtIndexPath and add a checkmark on the selected row it adds an additional checkmark. Here's my code

Any help would be very much appreciated.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    // Configure the cell...

    cell.textLabel.text=[[Model.category objectAtIndex:indexPath.row] categoryName];

    cell.imageView.image=[[Model.category objectAtIndex:indexPath.row]categoryImage];

    //cell.detailTextLabel.text =@"Breve Descripción de la categoria";

    return cell;

}

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

    if ([self.tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark) {

 [self.tableView cellForRowAtIndexPath:indexPath].accessoryType =UITableViewCellAccessoryNone;

 [self.cellSelected removeObject:indexPath];

    }else {

     [tableView cellForRowAtIndexPath:indexPath].accessoryType=UITableViewCellAccessoryCheckmark;

        [self.cellSelected addObject:indexPath];

    }

   [self checkMark];

   [tableView reloadData];   
}

- (void)checkMark{

    for (NSIndexPath * indexPath in self.cellSelected) {

       [self.tableView cellForRowAtIndexPath:indexPath].accessoryType=UITableViewCellAccessoryCheckmark;

    }


}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

[self.tableView cellForRowAtIndexPath:indexPath] call in the didSelectRowAtIndexPath will not return the exact cell. It can be same cell, new cell or reused cell. If it is a reused cell at its accessory view has a checkmark, you will end up having two cells with checkmark.

Its better to store in the array and use it accordingly. If you are planning to have multiple selections, Use the code example below.

- (void)viewDidLoad
{
    [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.
    self.cellSelected = [NSMutableArray array];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   //Cell Initialisation here

    if ([self.cellSelected containsObject:indexPath])
    {
      cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else
    {
      cell.accessoryType = UITableViewCellAccessoryNone;

    }
    return cell;
}


 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //if you want only one cell to be selected use a local NSIndexPath property instead of array. and use the code below
   //self.selectedIndexPath = indexPath;

    //the below code will allow multiple selection
    if ([self.cellSelected containsObject:indexPath])
    {
      [self.cellSelected removeObject:indexPath];
    }
    else
    {
       [self.cellSelected addObject:indexPath];
    }
    [tableView reloadData];
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...