UITableViewCell:如何在第五行更新textLabel.text?

Vol*_*da2 6 iphone objective-c uitableview ios

我创建并实现了UITableViewCell.当我按下UINavigationButton时,我想在第五行更新textLabel.text.我怎样才能做到这一点?

UITableViewCell *cell;
cell = [tableView_ dequeueReusableCellWithIdentifier:@"any-cell"];
if(cell == nil)
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"any-cell"] autorelease]; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.textLabel.text = leftString;
Run Code Online (Sandbox Code Playgroud)

Emp*_*ack 15

直接的方式是,

NSIndexPath *fifthRow = [NSIndexPath indexPathForRow:4 inSection:0];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:fifthRow];
cell.textLabel.text = @"the updated text";
Run Code Online (Sandbox Code Playgroud)

但是,更好的方法是更新dataSource并重新加载tableView或只是行.

  • 直接更新单元格中的标签可能看起来比有时通过表格方法重新加载该单元格更好.但即使你这样做 - 你也必须**更新数据源. (2认同)