当用户点击表格视图中的行时,如何更新 UITableViewCell(原型单元格)中的标签? [英] How do I update a label in a UITableViewCell (prototype cell) when the user taps the row in the table view?

查看:25
本文介绍了当用户点击表格视图中的行时,如何更新 UITableViewCell(原型单元格)中的标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用表格视图(动态)创建一个简单的列表,表格视图单元格加载了来自简单数组的数据.

I am trying to create a simple list using a Table View (dynamic) with Table View Cells loaded with data from a simple array.

当用户点击表格视图中的一行时,我试图更新表格视图单元格上的标签.

I am trying to update a label on the Table View Cell when the user taps a row in teh Table View.

问题:由于某种原因,我可以更新所选单元格上的标签,但单元格变为灰色,我无法使用以下方法取消选择它:

ISSUE: for some reason I can update the lable on the selected cell but the cell turns GREY and I cannot DeSelect it using:

[tableView deselectRowAtIndexPath:indexPath animated:NO];

但如果我不更新表格视图单元格标签,则 DeSelect 有效!

but if I do NOT update the table view cell label, then the DeSelect works!

反之亦然,如果我删除 DeSelect 行语句,则标签更新有效!

And vise-versa, if I remove the DeSelect row statement then the label update works!

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

I cannot get them to work at the same time :(

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

Here is my code I use to update the label: - (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 创建新单元格,这就是您的文本不更新的原因.下面的代码应该可以解决您的问题.

There's no need to create a new cell with a dequeueReusableCellIdentifier in the didSelectRowAtIndexPath method, that's why your text isn't updating. This below code should solve your problems.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    ListCell *cell = [tableView cellForRowAtIndexPath:indexPath];   
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.TitleLabel.text = @"testing";
}

这篇关于当用户点击表格视图中的行时,如何更新 UITableViewCell(原型单元格)中的标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆