我一直在尝试为一些我认为不应该那么困难的事情找到一个简单的答案,但我要么找不到一个像样的答案,要么无法理解它。
我正在尝试更改/编辑一个特定表格 View 单元格的文本标签内容。因此,举例来说,我希望在 UITableView 之外但在同一 View Controller 中使用一个 UIButton
来更改第 2 行中的文本,同时保持其他行相同。
我知道您使用 viewWithTag
但除此之外我无法弄清楚!请帮忙....
这是我到目前为止的代码
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
EntryCell *cell = (EntryCell *)[contentTable dequeueReusableCellWithIdentifier:@"Cell"];
cell.mainLabel.tag = indexPath.row;
cell.mainLabel.text = @"hello";
return cell;
}
-(void)buttonPressed:(UIButton *)button {
cell.mainLabel = (UILabel *)[contentTable viewWithTag:2];
cell.mainLabel.text = @"goodbye";
}
最佳答案
不要使用viewWithTag
,使用下面的获取对单元格的引用
-(void)buttonPressed:(UIButton *)button
{
EntryCell *cell = (EntryCell *)[tblView cellForRowAtIndexPath: [NSIndexPath indexPathForRow:2 inSection:0]];
if(cell != nil)
{
cell.mainLabel.text = @"goodbye";
}
}
编辑
正如 david 所指出的,如果单元格不可见,这将被覆盖,因为 cellForRowAtIndexPath:
将在它变得可见时再次调用。用您指定的任何内容覆盖此更改。
在此之后您将需要更新您的数据模型,以便更改是永久性的
关于ios - 通过标签编辑 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22538742/