数据重新加载时,iOS Collection View 向上移动

标签 ios uitableview uicollectionview

我有一个管理两行数据的 TableView。每行都由 CollectionView 管理,它允许用户水平滚动并过滤数据。过滤器是 TableView 的一部分,位于 CollectionView 之上。

这是一个视觉效果:

enter image description here

当我单击第一行的过滤器(上图中屏幕外)时,用户界面会平滑地更新数据。但是,当我单击第二行的过滤器时, Collection View 会短暂向上移动,然后返回到正确的位置。

我相当有信心该问题与我的界面生成器配置有关,但郑重声明,这是重新加载 Collection View 的代码。

func onFetchCompleted() {
        if shouldRefreshRow() {
            //tableView.reloadData() // reload all rows
            tableView.reloadRows(at: [IndexPath(row: viewModel!.rowToFetch!, section: 0)], with: .none)
        }
}

func shouldRefreshRow() -> Bool {
        return self.viewModel?.businessesStore[viewModel!.rowToFetch!].previousPage == 1
    }

这是 TableView 配置:

func numberOfSections(in tableView: UITableView) -> Int {
        return 1
 }

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
 }

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        if let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as? BusinessesTableViewCell {
            cell.configureCell(dataSourceDelegate: self, filterDelegate: self, forPath: indexPath, indexPathsToReload: 
            return cell
        }

     return UITableViewCell()
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {


    if(viewModel?.businessesStore[collectionView.tag].businesses.count ?? 0 > 0) {
        return (viewModel?.businessesStore[collectionView.tag].total)!
        }
        return 0
    }

在界面生成器中,我将 TableView 的前导、尾随和底部约束相对于 super View 设置为 0,相对于 TableView 上方的 View (上图中的纯黑色)将顶部约束设置为 0。

我相当确定这是一个界面生成器问题,因为如果我删除底部约束并将 TableView 高度设置为 400 之类的值,则单击过滤器时 View 会正常运行。问题是用户必须向下滚动到屏幕底部,否则它会以上面解释的卡顿方式运行。

这是布局:

enter image description here

有什么想法吗?

最佳答案

经过一番坚持,我终于找到了解决这个问题的方法。我的配置中有两处错误。

1:当我使用这行代码重新加载数据时 tableView.reloadRows(at: [IndexPath(row: viewModel!.rowToFetch!, section: 0)], with: .none)它似乎在 Collection View 上包含某种反弹动画。我用 tableView.reloadData() 替换了它,它修复了发生的大部分反弹。

2:我增加了表格 View 行的大小,从而消除了其余的反弹。

关于数据重新加载时,iOS Collection View 向上移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57434373/

相关文章:

ios - 不同宽度的 UITableViewCell 自动布局

ios - 更改 SelectedRow 上的 UILabel

iphone - 最终 TableView :heightForRowAtIndexPath: solution

ios - UICollectionViewCell 重用

ios - UI 未在 SWRevealViewController viewWillAppear 上更新

ios - 如何在 SwiftUI 中使用多个选择器更改 UIPickerView 宽度?

iphone - 从 ios sdk 中的文档目录加载图像

ios - 向 collectionview 注册 nib

ios - 无法在 UITableView 中定义原型(prototype)单元格

ios - 使用 ZBar SDK 时 AVCapture 无法设置焦点