在didSelectRowAtIndexPath上更改iPhone tableview单元格标签文本

时间:2012-06-26 12:56:28

标签: iphone uilabel tableview didselectrowatindexpath

我想在didSelectRowAtIndexPath上更改自定义UITableviewCell文本的文本,我使用以下代码: -

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.lblName.text=@"cambridge";
[tableView deselectRowAtIndexPath:indexPath animated:NO];

 }

但我得到的是“请求成员'levelNo',而不是结构或联合”。但是我可以在cellForRowAtIndexPath中设置它。 请帮助

3 个答案:

答案 0 :(得分:3)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 YourCustomCell *cell = (YourCustomCell*)[tableView cellForRowAtIndexPath:indexPath];
 cell.lblName.text=@"cambridge";
 [tableView deselectRowAtIndexPath:indexPath animated:NO];

 //update your data, your data source must be mutable
 //in case your array content are NSString, just do
  [yourMutableArrayDataSource replaceObjectAtIndex:indexPath.row withObject:@"cambridge"];

 //or if your data array holds NSDictionary. you can just initialize new dictionary 
 //as a replacement of the object in case you dont want your dictionary to be mutable
 NSDictionary *tempDict = [NSDictionary dictionaryWithObjectsAndKeys:@"cambridge",@"text",@"yourOtherData",@"otherData" nil];
  [yourMutableArrayDataSource replaceObjectAtIndex:indexPath.row withObject:tempDict];

 }

Maulik (谢谢)所述,当单元格滚动时,lblName文本将更改回原始文本。您可能希望更新数据源以保留新数据。 **回答编辑

答案 1 :(得分:0)

(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath    
{
  UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
 cell.lblName.text=@"New text Here";
 [tableView deselectRowAtIndexPath:indexPath animated:NO];
 [tableView reloadData];
}

答案 2 :(得分:0)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
 YourCustomCell *cell = (YourCustomCell*)[tableView cellForRowAtIndexPath:indexPath];
 cell.lblName.text=@"cambridge";
 [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

通过 janusfidel 的上述代码,将更改标签的文字。但是当你滚动表格时,我猜值会改变为原始值。

如果要更改原始数据,还需要更新数据源(即阵列)。