【问题标题】:How to reload only the visible sections of a UICollectionView?如何仅重新加载 UICollectionView 的可见部分?
【发布时间】:2016-10-27 11:29:56
【问题描述】:

我正在尝试根据设备的方向更改部分中的项目数。

我通过在 viewWillTransitionToSize 中重新加载整个集合视图并更改 UICollectionView 中的部分数量中的“部分中允许的最大项目”的数量来实现它。

这不是最好的方式,因为我可以看到我们可以加载特定的部分。 如果您能提出更好的解决方案,请分享。

【问题讨论】:

  • 你得到了collectionView的可见项的indexPaths。你试过了吗?
  • 实际上只有可见的项目才会重新加载
  • 我试过这个@Adeel -(void)reloadVisibleSections { NSMutableIndexSet *visibleSections = [NSMutableIndexSet indexSet]; NSArray *arr = [[self.collectionView indexPathsForVisibleItems] valueForKey:@"section"]; for(NSNumber *index in arr) { [visibleSections addIndex:[index unsignedIntegerValue]]; } [self.collectionView reloadSections:visibleSections];当我们旋转设备时会崩溃,可能是因为当我从横向移动到纵向时可见部分的数量增加了。
  • @SanjayPathak 请使用代码和崩溃日志更新您的问题。

标签: ios objective-c uicollectionview


【解决方案1】:

您可以使用以下代码重新加载特定部分:

let table = UITableView()
let index = IndexSet.init(integer: 0) //The section to reload
table.reloadSections(index, with: UITableViewRowAnimation.automatic)

如果您想根据可见部分执行此操作,则需要像这样获取可见单元格: table.visibleCells

并创建要从这些可见单元重新加载的部分索引

let table = UITableView()
var indexes = IndexSet()
let visibleCells = table.visibleCells
for cell in visibleCells {
    indexes.insert((table.indexPath(for: cell)?.section)!)
}
table.reloadSections(indexes, with: UITableViewRowAnimation.automatic) 

【讨论】:

    【解决方案2】:
        NSSet *visibleSections = [NSSet setWithArray:[[self.tableView indexPathsForVisibleRows] valueForKey:@"section"]];
    

    //重新加载部分

    [self.myCollectionView reloadSections:visibleSections];

    【讨论】:

    • 上面的代码对tableView很好。对于 collectionView -(void)reloadVisibleSections { NSMutableIndexSet *visibleSections = [NSMutableIndexSet indexSet]; NSArray *arr = [[self.collectionView indexPathsForVisibleItems] valueForKey:@"section"]; for(NSNumber *index in arr) { [visibleSections addIndex:[index unsignedIntegerValue]]; } [self.collectionView reloadSections:visibleSections]; } 当我们旋转设备时会崩溃。
    猜你喜欢
    • 2013-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多