【问题标题】:Collectionview not showing收藏视图未显示
【发布时间】:2020-10-18 14:11:19
【问题描述】:

我正在尝试实现一个功能,即每当我从底部的集合视图中点击一个按钮时,被点击的特定值将反映在中间的集合视图上。到目前为止,我已经实现了底部的 collectionview,但我不确定我的新 collectionview 没有显示的代码有什么问题。


import UIKit
class MyButtonCell: UICollectionViewCell{
    @IBOutlet weak var buttonOne: UIButton!
    @IBOutlet weak var targetButton: UIButton!
    
    
    var callback: (() -> ())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
        
    }
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        commonInit()
    }
    func commonInit() -> Void {
        contentView.layer.borderWidth = 1
        contentView.layer.borderColor = UIColor.black.cgColor
        
        
        
        
        
    }
    
    @IBAction func buttonTapped(_ sender: UIButton) {
        callback?()
    }
}

class StevenViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {

    let buttonTitles: [String] = [
        "4", "6", "7", "8"
    ]
    
    var targetButtonTitles: [String] = []
    //number array
    
    @IBOutlet var collectionView: UICollectionView!
    
    @IBOutlet weak var targetCollection: UICollectionView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView.delegate = self
        collectionView.dataSource = self
        
        targetCollection.delegate = self
        targetCollection.dataSource = self
        
    }
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return buttonTitles.count
        
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCellID", for: indexPath) as! MyButtonCell
        
        let targetCell = targetCollection.dequeueReusableCell(withReuseIdentifier: "target", for: indexPath) as! MyButtonCell

        // set the button title (and any other properties)
     
        cell.buttonOne.setTitle(buttonTitles[indexPath.item], for: [])
     

        
        // set the cell's callback closure
        cell.callback = {
            print("Button was tapped at \(indexPath)")
            self.targetButtonTitles.append(self.buttonTitles[indexPath.item])
            print(self.targetButtonTitles)
        
            targetCell.targetButton.setTitle(self.targetButtonTitles[indexPath.item], for: [])
            // do what you want when the button is tapped
        }
        

        
        return cell
    }
}


我的故事板:

模拟器中显示的内容:

【问题讨论】:

  • 我看不到您在哪里重新加载收藏视图。

标签: ios swift xcode button uicollectionview


【解决方案1】:

试试这个。

在您的cellForItemAtIndexPath 中,您必须返回两个单元格才能同时显示它们。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCellID", for: indexPath) as! MyButtonCell

    let targetCell = targetCollection.dequeueReusableCell(withReuseIdentifier: "target", for: indexPath) as! MyButtonCell

    if collectionView == self.collectionView {
       
        // Setup here your cell

        cell.callback = {
            print("Button was tapped at \(indexPath)")
            self.targetButtonTitles.append(self.buttonTitles[indexPath.item])
            print(self.targetButtonTitles)
        
            targetCell.targetButton.setTitle(self.targetButtonTitles[indexPath.item], for: [])
            // do what you want when the button is tapped
        }

        cell.buttonOne.setTitle(buttonTitles[indexPath.item], for: [])

        return cell
    } else {

        // Setup here your targetCell

        return targetCell
    }

请注意,如果每个 collectionView 有不同数量的部分,则必须单独返回它们。

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if collectionView == self.collectionView {
        return 1 // Number of sections of your collectionView
    }

    return 1 // Number of sections of your targetCollection
}

【讨论】:

  • 嘿,好点子。感谢您发现这一点!!!但是,我想在按下单元格后返回 targetCell - 它似乎不起作用。
  • 欢迎您!然后你可能想看看didSelectRowAt 委托函数。但是,您仍然需要cellForItemAt 来返回您的targetCell,否则它不会显示。如果您需要更多详细信息,请写信给我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多