How to reach the current selected cell in didSelectRowAtIndexPath?

6 Solutions Collect From Internet About “How to reach the current selected cell in didSelectRowAtIndexPath?”

You can get the cell from didSelectRowAtIndexPath: method using the indexPath like this.

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
     UITableViewCell *myCell = [tableView cellForRowAtIndexPath:indexPath];
     // Access accessory View  as below.  
     UIView * myCellAccessoryView = myCell.accessoryView;
}

Use the below function and pass the index path of the selected row so that the particular cell is reloaded again.

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

Another solution: store the selected row index and do a reload of tableview. Then in cellForRowAtIndexPath check for the selected row and change the accessory view of the cell.

Swift

You can get the cell from didSelectRowAtIndexPath: method using the indexPath like this:

 let cell:UITableViewCell? = tableView.cellForRowAtIndexPath(indexPath)

In Swift 3.0 : You can use these 2 ways, according to your need

 let cell:UITableViewCell = tableView.cellForRow(at: indexPath) as UITableViewCell

or

let cell:CustomTableViewCell = tableView.cellForRow(at: indexPath) as! CustomTableViewCell

Happy Coding !!

Store selected indexpath. for eg:
NSInteger selectedIndexPath;
then follow the step below. set your accessory view.

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
    AddressTableViewCell *cell=(AddressTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    cell.backgroundColor=[UIColor lightGrayColor];

    Address *addr=[self.locationArray objectAtIndex:indexPath.row];

    cell.titleLabel.text = addr.addressTitle;
    cell.detailLabel.text = addr.addressDetail;

        if (indexPath.row == selectedIndexPath) {
            [cell.selectButton setImage:[UIImage imageNamed:@"checkboxFilled"] forState:UIControlStateNormal];
        }
        else {
            [cell.selectButton setImage:[UIImage imageNamed:@"checkbox"] forState:UIControlStateNormal];

        }

    return cell;
}


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

    selectedIndexPath=indexPath.row;

    [self.tableView reloadData];

}