【问题标题】:UICollectionView Can't use IBOutlets in ViewController.swift [duplicate]UICollectionView 不能在 ViewController.swift 中使用 IBOutlets [重复]
【发布时间】:2019-03-11 10:06:03
【问题描述】:

我按照随附的指南创建静态 UICollectionView,但现在我想为每个单元格添加按钮并更改按钮上的文本,例如。我无法执行此操作并收到错误“UIButton 无效。插座无法连接到重复内容。”如何解决此问题并在不离开 ViewController 的情况下将 IBOutlets 与单元格中的对象一起使用?

如果我需要离开 ViewController,请详细描述该过程,因为我是初学者,对不同的视图类不太了解。

谢谢!!

【问题讨论】:

    标签: ios swift uicollectionview iboutlet


    【解决方案1】:

    您应该创建UICollectionViewCell 的子类,而不是按钮和视图控制器之间的出口,并在该类上添加您的 IBOutlets。

    class MyCollectionViewCell: UICollectionViewCell {
        @IBOutlet var myButton: UIButton!
    }
    

    然后,在 Interface Builder 中,将此子类设置为您的单元格的类(在身份检查器窗格中)。

    然后您应该能够创建从按钮到单元的插座连接。

    我希望这已经足够清楚了。如果没有,请告诉我!

    示例代码

    class MyCollectionViewCell: UICollectionViewCell {
        @IBOutlet var myButton: UIButton!
    }
    
    class MyViewController: UIViewController, UICollectionViewDataSource {
        @IBOutlet var myCollectionView: UICollectionView!
    
        private var isMyButtonEnabled = true
    
        // Other view controller code
    
        func disableMyButton() {
            self.isMyButtonEnabled = false
            self.myCollectionView.reloadData()
        }
    
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = ... as! MyCollectionViewCell // Get cell
            // Other cell setup
    
            cell.myButton.isEnabled = self.isMyButtonEnabled
    
            return cell
        }
    }
    

    【讨论】:

    • 是的!太感谢了!我想我终于征服了 UICollectionView...通过...复制互联网上的人¯_(ツ)_/¯ 至少我正在学习哈哈。再次感谢!
    • 我已经完成了这项工作,并且它在我的项目中有效,但现在我需要使用在主 ViewController 类中执行的函数来调整单元格中按钮的属性。我该怎么做呢?
    • @TylerDakin 看看this tutorial。您可以向子类添加一个函数,然后在 cellForItem 数据源函数中调用该函数
    • 我确实使用它来设置 UICollectionView,我不记得它有过,但我会再看一下,谢谢。
    • 澄清一下,在我的程序中,当步进值更改时,会运行该函数来检查是否需要禁用按钮,如果需要,则禁用按钮。
    【解决方案2】:

    为您的集合视图定义如下类:

    class MyCollectionCell : UICollectionViewCell {
        @IBOutlet weak var likeButton: UIButton?
    }
    

    为集合单元创建 xib 并将上面的自定义类用于集合视图。

    现在在您的视图控制器中定义集合视图并实现以下委托UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout

    class ViewController: UIViewController, UICollectionViewDataSource, 
             UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    
        @IBOutlet var collectionView: UICollectionView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            let nib = UINib(nibName: "MyCollectionViewCell", bundle: nil)
            collectionView?.registerNib(nib, forCellWithReuseIdentifier: "myCell")
        }
    
        //UICollectionViewDelegateFlowLayout methods
        func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat
        {
            return 4;
        }
    
        func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat
        {
            return 1;
        }   
    
        //UICollectionViewDatasource methods
        func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int 
        {
            return 1
        }
    
        func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return 100
        }
    
        func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
            var cell = 
            collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as MyCollectionCell
            cell.likeButton.setTitle("myTitle", for: .normal)
            cell.likeButton.tag = indexPath.row
            cell.likeButton.addTarget(self, action: #selector(mainButton:), forControlEvents: .TouchUpInside)
            return cell
        }
    
        @IBAction func mainButton(sender: UIButton) {
          println(sender)
          // use button tag to find out which button is clicked.
        }
    }
    

    在上面的代码中,重要的方法是func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell,您可以在其中为按钮设置标签,然后使用该标签找出按下了哪个按钮,并使用该 id 找出您想要执行的数据源或操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-10
      • 1970-01-01
      • 2019-08-09
      • 2016-03-22
      • 1970-01-01
      • 1970-01-01
      • 2021-11-21
      相关资源
      最近更新 更多