当用户点击表格视图中的行时,如何更新UITableViewCell(原型单元格)中的标签?

我试图创build一个简单的列表使用表视图(dynamic)与表格视图单元格加载数据从一个简单的数组。

我正在尝试更新表视图单元格上的标签,当用户点击表视图中的行。

问题:出于某种原因,我可以更新所选单元格上的标签,但单元格变成灰色,我不能使用DeSelect:

[tableView deselectRowAtIndexPath:indexPath animated:NO]; 

但如果我不更新表视图单元格标签,那么DeSelect的作品!

反之亦然,如果我删除了DeSelect行语句,那么标签更新的作品!

我不能让他们同时工作:(

下面是我用来更新标签的代码: – (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

  static NSString *CellIdentifier = @"ListCell"; ListCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; cell.accessoryType = UITableViewCellAccessoryNone; cell.TitleLabel.text = @"testing"; 

有人有主意吗?

没有必要在didSelectRowAtIndexPath方法中使用dequeueReusableCellIdentifier创build一个新的单元格,这就是为什么您的文本没有更新。 这下面的代码应该能解决你的问题。

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; ListCell *cell = [tableView cellForRowAtIndexPath:indexPath]; cell.accessoryType = UITableViewCellAccessoryNone; cell.TitleLabel.text = @"testing"; } 

首先,获取选定的单元格使用cellForRowAtIndexPath方法

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; cell.TitleLabel.text = @"testing"; } 

其次,要禁用单元格的select样式,请在cellForRowAtIndexPath方法中将单元格的selectionStyle设置为UITableViewCellSelectionStyleNone