【问题标题】:Cell takes index of its previous cell - Collectionview inside Tableview单元格获取其前一个单元格的索引 - Tableview 内的 Collectionview
【发布时间】:2016-08-31 10:51:21
【问题描述】:

我想在 tableview 中动态添加集合视图。我已经制作了以下代码。

collectionview 的单元类

class NewsFeedCollectionViewCell : UICollectionViewCell{
    @IBOutlet weak var imageViewSlider: UIImageView!
}

比在 indexpath 的 Tableview cellforrow 中分配 collectionview

cell.collectionViewNewsFeed.tag = indexPath.row
cell.collectionViewNewsFeed.reloadData()

添加以下collectionview的代表

// MARK: - Collection view Delegates / Datasources

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return (mutableArrayNewsFeed[collectionView.tag]["images"] as! NSArray).count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("NewsFeedCollectionViewCell", forIndexPath: indexPath) as! NewsFeedCollectionViewCell
    print("tag : \(collectionView.tag) , Row : \(indexPath.row)")
    let img = (mutableArrayNewsFeed[collectionView.tag]["images"] as! NSArray)[indexPath.row] as? String ?? "imgLoginScreenLogo"
    cell.imageViewSlider.image = UIImage(named: img)
    return cell
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    print("Collection View Row : \(indexPath.row)")
}

func collectionView(collectionView: UICollectionView,
                    layout collectionViewLayout: UICollectionViewLayout,
                           sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize{
    return collectionView.frame.size
}

它可以正确调整,但是当我滚动 collectionview 时索引会发生变化。例如,我将 collectionview 滚动到 3 个单元格,而不是转到 tableview 的第 4 个索引,而不是将 collectionview 的第 4 个索引的索引设置为第 3 个。

只想在 Table 视图中添加包含多个图像的集合视图。我已经添加了,但是在将集合视图滚动到 tableview 的第一个单元格上的第三个图像后,我移动到 Tableview 的第 4 个单元格,collectionview 也自动滚动到第 4 个单元格。

需要摆脱困境。

【问题讨论】:

    标签: iphone swift uitableview uicollectionview uicollectionviewcell


    【解决方案1】:

    索引路径有问题,我通过将索引路径保存在数组中来获得解决方案。

    参考以下链接 https://github.com/ashfurrow/Collection-View-in-a-Table-View-Cell

    【讨论】:

      猜你喜欢
      • 2019-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-08
      • 1970-01-01
      相关资源
      最近更新 更多