ios - 通过标签编辑 UITableViewCell

标签 ios objective-c uitableview tags

我一直在尝试为一些我认为不应该那么困难的事情找到一个简单的答案,但我要么找不到一个像样的答案,要么无法理解它。

我正在尝试更改/编辑一个特定表格 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/

相关文章:

ios - 找不到 'glog/logging.h'文件怎么解决

ios - 从 NSNumber 到 NSString 的转换问题

ios - NSMutableArray removeObjectAtIndex 导致意外的 SIGABRT

objective-c - UITableViewCell 中的 UITextField 不会成为第一响应者

ios - cocos2d 中的等距 tilemap 平台游戏 - 玩家跳跃?

ios - 使用 Swift 中更新的 header Transmission 重新发送您的请求

ios - 将android代码移植到 objective-c

iphone - 如何让点击返回按钮后键盘消失

ios - 错误,核心数据,原因: '*** -[_PFArray objectAtIndex:]: index (2) beyond bounds (2)'

ios - iOS 中语言切换后未调用 Tableview cellForRowAt indexPath