How to reach the current selected cell in didSelectRowAtIndexPath?

前端 未结 6 1661
迷失自我
迷失自我 2021-02-01 00:10

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 t

相关标签:
6条回答
  • 2021-02-01 00:49

    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.

    0 讨论(0)
  • 2021-02-01 00:51

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

    0 讨论(0)
  • 2021-02-01 00:54
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
    {
         UITableViewCell *myCell = [tableView cellForRowAtIndexPath:indexPath];
         // Access accessory View  as below.  
         UIView * myCellAccessoryView = myCell.accessoryView;
    }
    
    0 讨论(0)
  • 2021-02-01 00:58

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

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    0 讨论(0)
  • 2021-02-01 00:59

    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];
    
    }
    
    0 讨论(0)
  • 2021-02-01 01:04

    Swift

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

     let cell:UITableViewCell? = tableView.cellForRowAtIndexPath(indexPath)
    
    0 讨论(0)
提交回复
热议问题