ios 如何在 didSelectRowAtIndexPath 中到达当前选定的单元格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6547065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:36:15  来源:igfitidea点击:

How to reach the current selected cell in didSelectRowAtIndexPath?

iphoneobjective-ciosxcode

提问by JaHelia

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 ?

我想通过方法更改单元的辅助视图类型:DidselectRoyatindexpath,即选择一行时,我想更改附件视图类型,我可以在该方法内完成吗?

回答by EmptyStack

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

您可以像这样使用indexPathdidSelectRowAtIndexPath:方法中获取单元格。

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

回答by Tejinder

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

在 Swift 3.0 中:您可以根据需要使用这两种方式

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

or

或者

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

Happy Coding !!

快乐编码!!

回答by Jhaliya

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

回答by Banu Prakash

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 cellForRowAtIndexPathcheck for the selected row and change the accessory view of the cell.

另一种解决方案:存储选定的行索引并重新加载 tableview。然后cellForRowAtIndexPath检查所选行并更改单元格的附件视图。

回答by odemolliens

Swift

迅速

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

您可以使用 indexPath 从 didSelectRowAtIndexPath: 方法中获取单元格,如下所示:

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

回答by Chandramani

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

存储选定的索引路径。例如:NSInteger selectedIndexPath; 然后按照以下步骤操作。设置您的附件视图。

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

}