簡體   English   中英

UICollectionView在UITableViewCell中重用

[英]UICollectionView reuse in UITableViewCell

我知道這個問題已經問了很多次,但是沒有解決方案可行。

我在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
    }

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
    }

}

我滾動的Cell 1st

在此輸入圖像描述

單元格4自動滾動

在此輸入圖像描述

有兩種情況。

1 - 重置UICollectionView偏移位置:

要重置collectionview位置,只需在cellForRowAtIndexPath中調用cell.collectionView.contentOffset = .zero

2 - 保持上一個滾動位置:

要保持上一個滾動位置,您需要在包含視圖控制器中的數據模型旁邊存儲每個單元格的偏移列表。 然后,您可以在didEndDisplayingCell保存給定單元格的偏移量,並將其設置為cellForRowAtIndexPath而不是.zero

UITableView致力於reusability的概念。 每當滾動tableView時, tableView中的cells tableView定會被重用。

現在出現了2個場景,

  1. 獲取一個新單元格 - 當重復使用tableViewCell ,其中的collectionView也將被重用,因此這就是保留前一個單元格的contentOffset的原因。

  2. Scroll to previous cell - 如果我們手動滾動到collectionView的特定cell ,我們可能希望在滾動回到它時保留其位置。

要在tableView獲得這種功能,我們需要 - 為1st case手動重置contentOffset ,並且需要在第二種1st case保留contentOffset

您可以遵循的另一種方法是使用combination of UIScrollview and UIStackViewcombination of UIScrollview and UIStackView

這是怎么回事。

  1. storyboard ,創建一個UIScrollView。 添加一個垂直的UIStackView。

  2. UICollectionViews中添加4個stackView 此編號可根據您的要求進行配置。

  3. controller添加為所有collectionViewsdataSourcedelegate

class ViewController: UIViewController, UICollectionViewDataSource {
    override func viewDidLoad() {
        super.viewDidLoad()
    }

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CustomCell
        cell.label.text = "\(indexPath.row)"
        return cell
    }
}

class CustomCell: UICollectionViewCell {
    @IBOutlet weak var label: UILabel!
}

現在,由於我們使用的是stackView ,因此不會重復使用任何collectionViews 因此,所有collectionViewscontentOffsets將保持不變。

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM