【问题标题】:change iPhone tableview cell label text on didSelectRowAtIndexPath在 didSelectRowAtIndexPath 上更改 iPhone 表格视图单元格标签文本
【发布时间】:2012-06-26 12:56:27
【问题描述】:

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

【问题讨论】:

    标签: iphone uilabel tableview didselectrowatindexpath


    【解决方案1】:

    试试

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

    【讨论】:

      【解决方案2】:
      (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];
      }
      

      【讨论】:

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

        通过上述janusfidel的代码,标签的文字会发生变化。但是当你滚动表格时,我猜值会变成原来的值。

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

        【讨论】:

          猜你喜欢
          • 2023-03-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-05-13
          相关资源
          最近更新 更多