【问题标题】:how can i use withReuseIdentifier我如何使用 withReuseIdentifier
【发布时间】:2017-09-22 10:25:02
【问题描述】:

如何使用多个withReuseIdentifier

这是代码 因为我有 4 个按钮不起作用 当我使用标识符“1”时,其他按钮不起作用

这是代码

extension ViewController : UICollectionViewDataSource
{
func numberOfSections(in collectionView: UICollectionView) -> Int
{
    return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
    return interests3.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "1", for: indexPath as IndexPath) as! interestCollectionViewCell
    cell.interest2 = self.interests3[indexPath.item]
    return cell
}

新代码

    import UIKit

class interestCollectionViewCell: UICollectionViewCell
{

    var interest2: Interest1! {
        didSet {
            updateUI()

        }

    }
    @IBOutlet weak var futerdimageview: UIImageView!
    @IBOutlet weak var interstTitleLabel: UILabel!


    private func updateUI()
    {
        interstTitleLabel.text! = interest2.title
        futerdimageview.image = interest2.featuredImage!


    }
    override func layoutSubviews() {
        super.layoutSubviews()
        self.layer.cornerRadius = 10.0
        self.clipsToBounds = true
    }
}

还有这段代码

    import UIKit

class Interest1
{
    var title = ""
    var description = ""
    var featuredImage: UIImage!
    var button1: UIButton!
    var button2: UIButton!
    var button3: UIButton!
    var button4: UIButton!



    init(title: String, featuredImage: UIImage!)
    {
        self.title = title
        self.featuredImage = featuredImage



    }

    static func createInterest() -> [Interest1]
    {

        return [
           Interest1(title: "One", featuredImage: UIImage(named:"001.png")!),
            Interest1(title: "Two", featuredImage: UIImage(named:"002.png")!),
            Interest1(title: "Three", featuredImage: UIImage(named:"003.png")!),
            Interest1(title: "Four", featuredImage: UIImage(named:"004.png")!),




        ]

    }
       }

【问题讨论】:

  • 用 indexPath 检查条件
  • 正是@chirag90
  • @chirag90 我试过这段代码,但我想我不知道如何使用它
  • @faten 在线程中显示您需要使用不同的重用标识符注册单元。这需要放入 viewdidload 方法中。在collectionview cellForItemAt 中,您可以开始使用其他标识符

标签: ios swift xcode uicollectionview uicollectionviewcell


【解决方案1】:

试试这个,在你的 viewDidLoad 方法中添加

self.collectionView.register(interestCollectionViewCell.self, forCellWithReuseIdentifier: "1")
self.collectionView.register(interestCollectionViewCell.self, forCellWithReuseIdentifier: "2")
self.collectionView.register(interestCollectionViewCell.self, forCellWithReuseIdentifier: "3")
self.collectionView.register(interestCollectionViewCell.self, forCellWithReuseIdentifier: "4")

并更改您的 collectionView cellForItemAt

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "1", for: indexPath as IndexPath) as? interestCollectionViewCell
        {
            cell.interest2 = self.interests3[indexPath.item]
            return cell
        } else if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "2", for: indexPath as IndexPath) as? interestCollectionViewCell
        {
            cell.interest2 = self.interests3[indexPath.item]
            return cell

        } else if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "3", for: indexPath as IndexPath) as? interestCollectionViewCell
        {
            cell.interest2 = self.interests3[indexPath.item]
            return cell

        } else if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "4", for: indexPath as IndexPath) as? interestCollectionViewCell
        {
            cell.interest2 = self.interests3[indexPath.item]
            return cell
        }
     return interestCollectionViewCell()
}

【讨论】:

  • 在一个函数中缺少返回 expexted 以返回 'UICollectionViewCell' 我收到了这个错误
  • interestCollectionViewCell类中出现错误:(`private func updateUI() { interstTitleLabel.text = interest2.titlefuterdimageview.image = interest2.featuredImage!}`
  • 我喜欢这个youtube.com/watch?v=JG7mWFcU0vk 我想要带有按钮的图像看视频,你会明白我想要什么
  • 在 collectionview 的 if 语句中,您需要根据需要对其进行修改,因为目前我刚刚使用了您拥有的那个。我假设它们不会都是相同的 cell.interest2 = self.interests3[indexPath.item]
  • 我在 interestCollectionViewCell 类中遇到此错误,错误是“致命错误:在展开可选值 (lldb) 时意外发现 nil”我不知道为什么
猜你喜欢
  • 2010-11-30
  • 2017-01-09
  • 2011-10-27
  • 2021-08-17
  • 2011-03-30
  • 2010-09-18
  • 2014-04-15
  • 2017-08-02
  • 2023-04-08
相关资源
最近更新 更多