【问题标题】:iOS Collection View shifts up when data reloads数据重新加载时 iOS 集合视图上移
【发布时间】:2019-08-09 16:57:58
【问题描述】:

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

这是一个视觉效果:

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

我相当确信这个问题与我的界面构建器配置有关,但为了记录,这是重新加载集合视图的代码。

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
    }

在界面构建器中,我将表视图的前导、尾随和底部约束设置为相对于超级视图为 0,相对于表视图上方的视图设置为顶部为 0(上图中的纯黑色)。

我相当肯定这是一个界面构建器问题,因为如果我移除底部约束并将我的 TableView 高度设置为 400 之类的值,则在单击过滤器时视图会正常运行。需要注意的是,用户必须向下滚动到屏幕底部,否则它会以上面解释的 janky 方式运行。

这是布局:

有什么想法吗?

【问题讨论】:

    标签: ios uitableview uicollectionview


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-01
      • 2019-01-31
      • 1970-01-01
      • 2021-05-25
      • 2018-11-05
      • 2019-07-08
      相关资源
      最近更新 更多