【问题标题】:Reload specific UITableView cell in iOS在 iOS 中重新加载特定的 UITableView 单元格
【发布时间】:2011-08-12 13:26:18
【问题描述】:

我有一个UITableView。我想根据所做的选择更新表数据。现在我使用[mytable reloadData];我想知道是否有任何方法可以更新所选的特定单元格。我可以使用 NSIndexPath 或其他方式进行修改吗?有什么建议吗?

谢谢。

【问题讨论】:

    标签: iphone ios uitableview


    【解决方案1】:

    对于 iOS 3.0 及以上版本,您只需调用:

    - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
    

    例如,要重新加载第 2 节的第 3 行和第 3 节的第 4 行,您必须这样做:

    // Build the two index paths
    NSIndexPath* indexPath1 = [NSIndexPath indexPathForRow:3 inSection:2];
    NSIndexPath* indexPath2 = [NSIndexPath indexPathForRow:4 inSection:3];
    // Add them in an index path array
    NSArray* indexArray = [NSArray arrayWithObjects:indexPath1, indexPath2, nil];
    // Launch reload for the two index path
    [self.tableView reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationFade];
    

    【讨论】:

    • 我不熟悉索引路径。能不能详细说一下。
    • NSIndexPath 顾名思义就是一个索引。索引由 2 个组件行和部分组成。您可以使用 [NSIndexPath indexPathForRow:(NSUInteger) inSection:(NSUInteger)]; 创建一个
    • 每个部分可以有多行。例如,在 iPod MP3 播放器中,每个字母都有一个部分。每个部分都包含几位艺术家。我已经编辑了我的原始代码以向您展示如何操作。
    【解决方案2】:

    您还可以获取对 UITableViewCell 对象的引用并更改其标签等。

    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    cell.textLabel.text = @"Hey, I've changed!";
    

    【讨论】:

      【解决方案3】:

      我想你正在寻找UITableView的这种方法:

       - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-05-25
        • 2021-03-21
        • 1970-01-01
        • 1970-01-01
        • 2011-12-23
        • 2011-07-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多