1

我想更改 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。 请帮忙

4

3 回答 3

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 文本将变回其原始文本。您可能想要更新数据源以保留新数据。**答案已编辑

于 2012-06-26T12:58:04.390 回答
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];
}
于 2012-06-26T13:06:07.083 回答
0
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
 YourCustomCell *cell = (YourCustomCell*)[tableView cellForRowAtIndexPath:indexPath];
 cell.lblName.text=@"cambridge";
 [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

通过上面的janusfidel代码,标签的文本将被更改。但是当您滚动表格时,我猜值会更改为原始值。

当您想要更改原始数据时,您还需要更新您的数据源(即您的数组)。

于 2012-06-26T13:10:12.877 回答