TableView内部的CollectionView scrollViewDidEndDecelerating崩溃

时间:2019-01-29 07:10:02

标签: swift scroll uicollectionview tableview

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

2 个答案:

答案 0 :(得分:0)

首先,我建议避免将UITableView粘在UICollectionView内-单元重用始终是一个坏主意,并且只会使事情陷入混乱。尝试使用IGListKit-它有助于轻松管理嵌套的数据结构,查看其示例。

对于您的代码-进行嵌套设置的方法-是让一个UICollecionViewCell容纳整个UITableView,而不是尝试计算表单元格的数量并将此数字传递给UICollectionView委托方法。因此-仅返回1个单元用于“收集视图”部分:

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

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

答案 1 :(得分:0)

您的表格视图当时似乎没有显示第一个单元格。因此,如果它不可见,那么对于第0行,您总会得到nil。

获取当时的当前可见行。然后,您将获得单元格对象。

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