How to reach the current selected cell in didSelectRowAtIndexPath?

IphoneObjective CIosXcode

Iphone Problem Overview


I want to change the accessory view type of a cell through the method: didSelectRowAtIndexPath , i.e. when a row is selected I want to change the accessory view type, can I do this inside that method ?

Iphone Solutions


Solution 1 - Iphone

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

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

Solution 2 - Iphone

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 !!

Solution 3 - Iphone

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

Solution 4 - Iphone

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.

Solution 5 - Iphone

Swift

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

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

Solution 6 - Iphone

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];
    
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJaHeliaView Question on Stackoverflow
Solution 1 - IphoneEmptyStackView Answer on Stackoverflow
Solution 2 - IphoneTejinderView Answer on Stackoverflow
Solution 3 - IphoneJhaliya - Praveen SharmaView Answer on Stackoverflow
Solution 4 - IphoneBanu PrakashView Answer on Stackoverflow
Solution 5 - IphoneodemolliensView Answer on Stackoverflow
Solution 6 - IphoneChandramaniView Answer on Stackoverflow