【发布时间】:2011-09-26 16:27:19
【问题描述】:
我想通过方法更改单元的附件视图类型:didselectrowatindexpath,即选择了一行时,我想更改附件视图类型,我可以在该方法内部吗?
【问题讨论】:
标签: iphone objective-c ios xcode
我想通过方法更改单元的附件视图类型:didselectrowatindexpath,即选择了一行时,我想更改附件视图类型,我可以在该方法内部吗?
【问题讨论】:
标签: iphone objective-c ios xcode
您可以像这样使用 indexPath 从 didSelectRowAtIndexPath: 方法中获取单元格。
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
【讨论】:
[tableView reloadData] 在 didSelectRowAtIndexPath: 方法中重新加载表视图?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell *myCell = [tableView cellForRowAtIndexPath:indexPath];
// Access accessory View as below.
UIView * myCellAccessoryView = myCell.accessoryView;
}
【讨论】:
使用以下函数并传递所选行的索引路径,以便再次重新加载特定单元格。
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
另一种解决方案:存储选定的行索引并重新加载 tableview。然后在cellForRowAtIndexPath 中检查选定的行并更改单元格的附件视图。
【讨论】:
斯威夫特
您可以使用 indexPath 从 didSelectRowAtIndexPath: 方法获取单元格,如下所示:
let cell:UITableViewCell? = tableView.cellForRowAtIndexPath(indexPath)
【讨论】:
存储选定的索引路径。例如: 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];
}
【讨论】:
在 Swift 3.0 中:您可以根据需要使用这两种方式
let cell:UITableViewCell = tableView.cellForRow(at: indexPath) as UITableViewCell
或
let cell:CustomTableViewCell = tableView.cellForRow(at: indexPath) as! CustomTableViewCell
编码愉快!!
【讨论】: