【问题标题】:How do you change the textLabel when UITableViewCell is selected?选择 UITableViewCell 时如何更改 textLabel?
【发布时间】:2010-12-23 00:48:34
【问题描述】:

我想在单元格被选中时更改它的 textLabel 和 detailTextLabel。 我尝试了以下方法,但没有发生任何变化:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    MyAppDelegate *appDelegate = (MyPhoneAppDelegate*)[[UIApplication sharedApplication] delegate];

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.detailTextLabel.text = @"xxxxx";
    cell.textLabel.text =       @"zzzzz";
    [tableView reloadData];
}

【问题讨论】:

    标签: iphone uitableview didselectrowatindexpath


    【解决方案1】:

    我同意,重新加载表格视图实际上会使用 tableView:cellForRowAtIndexPath: 转储和重新加载/显示所有单元格并使用原始数据,而不是 tableView:didSelectRowAtIndexPath: 方法中更新的 @"xxxxx" 和 @"yyyyy"。

    在一个小测试项目中,我能够在选择时更改标签:

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

    【讨论】:

      【解决方案2】:

      您不应该在选择单元格时尝试重新加载表格。相反,尝试

      [单元集需要布局]

      对标签进行上述更改后。

      另外,您在方法中引用应用委托是否有原因?

      【讨论】:

        【解决方案3】:

        尝试重新加载您选择的单元格(由 indexPath 描述):

        [yourTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
        

        【讨论】:

          【解决方案4】:

          创建一个新的 iPad 项目(拆分视图),现在通过 Classes->Files。那里给出了最简单的方法。 XCode 的生成代码。

          示例代码行:-

          cell.textLabel.text = [NSString stringWithFormat:@"Row %d", indexPath.row];
          

          您可以在cellForRowAtIndexPath ||&& didSelectRowAtIndexPath ..中使用它们

          【讨论】:

            【解决方案5】:

            不确定您要对委托做什么,但您应该尝试调用已实例化的 tableView;即调用

            UITableViewCell *cell = [self.tableView cellForRowAtIndexPath: indexPath];
            

            也许我不清楚 我的意思是你正在实例化一个新的空表视图

            UITableViewCell *cell = [**tableView** cellForRowAtIndexPath: indexPath]; //cell has nothing it is new.
            

            考虑替换为旧的

            UITableViewCell *cell = [**self.tableView** cellForRowAtIndexPath: indexPath]; //now you have one that has a textField already in it
            

            【讨论】:

            • 你明白我在说什么吗?您正在创建一个新实例而不是旧实例。
            • 如果您创建一个新的,您必须重新加载数据才能在表格视图中看到它。如果您使用旧视图,则视图将反映更改。
            【解决方案6】:

            您是否尝试仅刷新选定的单元格而不是重新加载整个表格?

            [cell setNeedsDisplay];

            而不是

            [tableView reloadData];

            这将有更好的性能,我不是,但我想如果你刷新整个表,选择就会丢失(这可能是你最终看不到任何变化的原因)......

            【讨论】:

            • 如果您使用自定义单元格视图会怎样?
            • 自定义单元格视图是设置为 UITableViewCell 的 contentView 的视图,所以同理,setNeedsDisplay 会重绘 contentView。
            猜你喜欢
            • 1970-01-01
            • 2013-02-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-11-12
            • 2011-06-10
            • 1970-01-01
            相关资源
            最近更新 更多