【问题标题】:CollectionView inside a TableView scrollViewDidEndDecelerating crashTableView scrollViewDidEndDecelerating 崩溃中的 CollectionView
【发布时间】:2019-01-29 07:10:01
【问题描述】:

在UITableView(水平滚动)numberOfRowsInSection 是动态数字。在UICollectionView(垂直滚动)numberOfItemsInSection 也是动态的。

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return (self.arrayNewsfeedsList[intIndex]as! Newsfeeds).media!.count//3
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "sliderCollectionViewCell", for: indexPath) as! sliderCollectionViewCell
    let stringImageUrl = "https://img.youtube.com/vi/CQElIZzzlpc/hqdefault.jpg"
    cell.imageViewSlider.setKRImageUrl(url:stringImageUrl) { (imageUser) in
        cell.imageViewSlider.image = imageUser
    }       
    return cell
}

在滚动 scrollViewDidEndDecelerating 和 scrollViewDidEndScrollingAnimation 时,应用程序崩溃了。

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    let cell = tableviewNewsfeed.cellForRow(at: NSIndexPath(row: 0, section: intIndex) as IndexPath)as! NewsfeedTableViewCell
    cell.pagecontrolYoutube?.currentPage = Int(scrollView.contentOffset.x) / Int(scrollView.frame.width - 50)
}

func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
    let cell = tableviewNewsfeed?.cellForRow(at: NSIndexPath(row: 0 , section: intIndex) as IndexPath)as! NewsfeedTableViewCell
    cell.pagecontrolYoutube?.currentPage = Int(scrollView.contentOffset.x) / Int(scrollView.frame.width - 50)
} 

如何更新这一行?

tableviewNewsfeed?.cellForRow(at: NSIndexPath(row: 0 , section: intIndex) as IndexPath)as! NewsfeedTableViewCell

请参阅此图片以供参考:https://imgur.com/a/oG4nLtc

【问题讨论】:

  • 那么nil 到底是什么?当编译器停止时,请在控制台中输入:po tableviewNewsFeed
  • > 链接 po tableviewNewsFeed: imgur.com/a/NmiVqyb
  • 在 cellForRowAt 中,我更新了 Indexpath.section 。 intIndex = indexPath.section
  • 这打印什么? po tableviewNewsfeed.cellForRow(at: IndexPath(row: 0, section: intIndex))
  • >输出:无。如何在滚动 tableview 时更新 Indexpath 值?

标签: swift scroll uicollectionview tableview


【解决方案1】:

首先,我建议避免将 UITableView 粘贴在 UICollectionView 中 - 重复使用单元格总是一个坏主意,它只会在某个地方搞砸。尝试使用 IGListKit - 它有助于轻松管理嵌套数据结构,查看他们的示例。

至于您的代码 - 进行嵌套设置的方法 - 是有一个 UICollecionViewCell 来保存整个 UITableView 而不是尝试计算表格单元格的数量并将这个数字传递给 UICollectionView 委托方法。所以 - 只为集合视图部分返回 1 个单元格:

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

然后在 UICollectionViewCell 子类中完全管理 UITableView 设置

【讨论】:

    【解决方案2】:

    看起来您的表格视图当时没有显示第一个单元格。因此,如果它不可见,那么第 0 行总是为零。

    获取当时的当前可见行。然后你会得到单元格对象。

    tableviewNewsfeed?.cellForRow(at: NSIndexPath(row: currentVisibleRow , section: intIndex) as IndexPath)as! NewsfeedTableViewCell
    

    【讨论】:

    • 好吧好吧。代码中的 currentVisibleRow 是什么以及如何获取 tableview ?
    • 表格视图单元格作为页面控制的页面工作?然后你可以根据你已经计算的当前页面计算它。或 tableView.indexPathsForVisibleRows 这将为您提供所有可见的索引路径
    猜你喜欢
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    • 2017-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多