【问题标题】:Reload UITableView without reloading header section重新加载 UITableView 而不重新加载标题部分
【发布时间】:2016-05-09 11:35:39
【问题描述】:

我有一个带有部分和索引标题的UITableview。当用户点击一个单元格时,高度为 42 的选项视图会显示在该单元格中。为了显示该选项,我重新加载了单元格。但是部分标题也正在更新。这给出了奇怪的动画。如何仅重新加载单元格,而无需调用其他委托方法,例如 heightForHeaderInSectionviewForHeaderInSection

【问题讨论】:

  • 你写了什么代码来重新加载tableview。以及您到底想做什么。您只想重新加载单元格或部分?
  • 使用begin/endupdate重新加载单元格

标签: ios swift uitableview


【解决方案1】:

我认为你应该只更新 indexPath 的特定行,试试下面的代码

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];

    [tableView beginUpdates];
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    [tableView endUpdates];

}

也许对你有帮助。

干杯。

【讨论】:

    【解决方案2】:

    在 Swift 2.0 及更高版本中

        func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    
            tableView.beginUpdates() 
            tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
            tableView.endUpdates()
    
           }
    

    【讨论】:

      【解决方案3】:

      有几种方法可以重新加载 tableview。

      1. 重新加载整个部分,包括部分的单元格。

        - (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation

      示例:

      [tableVIewContent reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationAutomatic];
      
      1. 重新加载单个/多个单元格

        - (void)reloadRowsAtIndexPaths:(NSArray<NSIndexPath > )indexPaths withRowAnimation:(UITableViewRowAnimation)animation

      示例:

      [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:1]] withRowAnimation:UITableViewRowAnimationFade];
      
      1. 重新加载整个表格视图。

        - (void)reloadData;

        // 从头开始​​重新加载所有内容。重新显示可见行。因为我们只保留有关可见行的信息,所以这很便宜。如果表格缩小,将调整偏移量

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-01
        相关资源
        最近更新 更多