UITableViewCell中的UICollectionView重用

chi*_*hah 8 uitableview ios uicollectionview swift

我知道这个问题已经被问过很多遍了,但是没有一个解决方案会起作用。

我在uitableview单元格中有集合视图,并且在情节提要中准备了表格视图和集合视图的单元格,我的问题是假设我滚动了单元格1集合视图,然后单元格4集合也自动滚动到了该位置。我如何处理这种情况。

请注意,现在我对屏幕进行静态设计

我的密码

// MARK:
    // MARK: table view delegate method

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

        if Appconstant.isIpad()
        {
            return 215.0
        }
        return 185.0

    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 6
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let strCell = "WorkoutCell" //+ "\(indexPath.row)"
        let cell = tableView.dequeueReusableCellWithIdentifier(strCell) as? WorkoutCell
//        if cell == nil
//        {
//            cell = work 
//        }
        return cell!
    }
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        let cell2 = cell as! WorkoutCell
        cell2.collecionWorkout.reloadData()
    }
    // MARK:
    // MARK: collectionview delegate and data source
    //1
    func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool {
        return true
    }
     func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }

    //2
     func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsetsMake(0.0, 10.0, 0.0, 10.0)
    }

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: NSIndexPath) -> CGSize {
        // Adjust cell size for orientation
        if Appconstant.isIpad() {
            return CGSize(width: 160, height: 150)
        }
        return CGSize(width: 140.0, height: 130.0)
    }
    //3
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("WorkoutCollectionCell", forIndexPath: indexPath)
        // Configure the cell
        return cell
    }
Run Code Online (Sandbox Code Playgroud)

uitableview单元格代码

class WorkoutCell: UITableViewCell {

    @IBOutlet weak var collecionWorkout: UICollectionView!
    var flowLayout : UICollectionViewFlowLayout!
    override func awakeFromNib() {
        super.awakeFromNib()
        self.flowLayout = UICollectionViewFlowLayout()
        if Appconstant.isIpad()
        {
            self.flowLayout.itemSize = CGSize(width: 160, height: 150)
        }
        else
        {
            self.flowLayout.itemSize = CGSize(width: 140, height: 130)
        }
        self.flowLayout.scrollDirection = .Horizontal
        self.flowLayout.minimumInteritemSpacing = 10.0
        flowLayout.sectionInset = UIEdgeInsetsMake(0.0, 10.0, 0.0, 10.0);
        collecionWorkout.collectionViewLayout = flowLayout

        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
Run Code Online (Sandbox Code Playgroud)

我滚动的第一格

在此处输入图片说明

自动滚动的第4个单元格

在此处输入图片说明

Tai*_*man 10

有两种情况。

1 - 重置 UICollectionView 偏移位置:

要重置 collectionview 位置,只需调用cell.collectionView.contentOffset = .zerocellForRowAtIndexPath

2 - 保持以前的滚动位置:

为了保持之前的滚动位置,您需要在包含视图控制器中的数据模型旁边存储每个单元格的偏移量列表。然后,您可以将给定单元格的偏移量保存在 中didEndDisplayingCell并将其设置cellForRowAtIndexPath.zero