ios - 在 tableView 中重新加载不可见部分的数据

标签 ios objective-c uitableview

然后我得到 json 更新我重新加载我的 tableView,但是我有 tableView 里面有 collectionView 并且在重新加载时我在可见单元格中几乎没有滞后。我只想重新加载 tableView 的不可见单元格。但是没有像“visibleCells”这样的“visibleSections”方法。

我得到了所有可见的部分

 NSArray *visibleSections = [[self.tableView indexPathsForVisibleRows]valueForKey:@"section"];

不是我想要获取所有部分,然后删除所有 visibleSections 并重新加载这个新数组。但是我找不到获取所有部分的方法。我该怎么做?

最佳答案

您可以使用以下方法之一:

- (void)reloadRowsAtIndexPaths:withRowAnimation:
- (void)reloadSections:withRowAnimation:

编辑

当您尝试获取所有不可见单元格的索引路径时,您可以尝试如下操作:

NSMutableArray *invisibleCells = [NSMutableArray array];

for (int i = 0; i < [tabsTableView numberOfSections]; i++) {
    for (int n = 0; n < [tabsTableView numberOfRowsInSection:i]; n++) {
        for (NSIndexPath *indexPath in [tabsTableView indexPathsForVisibleRows]) {
            if ([indexPath section] == i && [indexPath row] == n) {
                goto nextIteration;
            }
        }
        
        [invisibleCells addObject:[NSIndexPath indexPathForRow:n inSection:i]];
            
    nextIteration:;
    }
}

更重要的是你为什么要这样做?!由于 UITableViewCell 被重用,它们将不可更新。当单元格“不可见”时,它会更改其内容并在可见单元格内的不同位置使用。这就是 UITableView 的工作原理。

您将(目前也是)使用此方法来设置单元格的内容:

- (void)tableView:tableView cellForRowAtIndexPath:indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:_cellID];
    
    if (!cell) {
        cell = (UITableViewCell *)[[[NSBundle mainBundle] loadNibNamed:@"cellTemplate" owner:nil options:nil] objectAtIndex:0];
    }
    
    // configure cell content here
}

当单元格被重用时,仅当 UITableView 不重用不可见单元格时,才会调用此方法并创建单元格。如果是,则更改内容。

所以不需要重新加载不可见的单元格,这是不可能的。 From the docs :

Return Value

An object representing a cell of the table or nil if the cell is not visible or indexPath is out of range.

关于ios - 在 tableView 中重新加载不可见部分的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24336856/

相关文章:

ios - 在 UITableView cellForRowAtIndexPath 方法中访问 NSLayoutConstraint ?

iOS 8 修改苹果默认键盘?

objective-c - 是否可以制作一个数组的数组

ios - 具有多列的 TableViewCell

ios - 如何在同一框架内访问 Objective-C 中的内部 Swift 类?

ios - 如何在 UIWebView 中显示本地网络 url 的身份验证挑战?

ios - 重新创建谷歌收件箱的 ios 加号按钮

ios - 委托(delegate)可以用任何语言实现还是特定于语言

ios - 为什么当我从我转至的 View Controller 返回时,我的 UITableView 的格式完全出错?

iphone - Objective-C 中的委托(delegate)和通知