-1

How can I use multiple withReuseIdentifier ?

and here is the code because i have 4 button doesn't work when i use Identifier "1" the other buttons dosen't wrok

and here is the code

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
}

new code

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

and this code

    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")!),




        ]

    }
       }
faten
  • 25
  • 6
  • Check conditions with indexPath – Mukesh Sep 22 '17 at 10:27
  • 1
    have a look at this [Registering multiple cells in UICollectionView (swift 3)](https://stackoverflow.com/a/43971614/4056108) – chirag90 Sep 22 '17 at 10:27
  • exactly @chirag90 – Mukesh Sep 22 '17 at 10:28
  • @chirag90 i tryed this code but i think i don't know how to use it – faten Sep 22 '17 at 10:47
  • @faten in the thread it shows you need to register the cell with different reuse identifier. This needs to be put into viewdidload method. and in the collectionview cellForItemAt you can than start using the other identifiers – chirag90 Sep 22 '17 at 10:55
  • i put the identifier , but when i run my app only one button work the first button i type ` if ((interests3[indexPath.item].button1) != nil) { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "1", for: indexPath) as! interestCollectionViewCell let model = interests3[indexPath.item] return cell }` – faten Sep 22 '17 at 10:57
  • @chirag90 is there any Error ? – faten Sep 22 '17 at 10:58
  • have you used different reuseable name for the other buttons? – chirag90 Sep 22 '17 at 10:59
  • @chirag90 yes i use "1" & "2" & "3" & "4" – faten Sep 22 '17 at 11:00

1 Answers1

0

try this, in your viewDidLoad method add

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")

And change your 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()
}
chirag90
  • 2,061
  • 1
  • 19
  • 34
  • Missing return in a function expexted to return 'UICollectionViewCell' i got this error – faten Sep 22 '17 at 11:17
  • in the class `interestCollectionViewCell` i got error :( ` private func updateUI() { interstTitleLabel.text = interest2.title futerdimageview.image = interest2.featuredImage! }` – faten Sep 22 '17 at 11:24
  • i do like this https://www.youtube.com/watch?v=JG7mWFcU0vk i want the image with button see the video and you will understand me what i want – faten Sep 22 '17 at 11:40
  • inside the if statements in collectionview you will need to modify it to your needs, as currently i have just used the one you had.. And i am assuming they wont all be the same `cell.interest2 = self.interests3[indexPath.item]` – chirag90 Sep 22 '17 at 11:53
  • i got this Error in class `interestCollectionViewCell ` and the error is 'fatal error: unexpectedly found nil while unwrapping an Optional value (lldb) ' i don't know why – faten Sep 22 '17 at 12:03
  • You need to start debugging and doing google search, there are many questions on how to resolve optional values. – chirag90 Sep 22 '17 at 12:05
  • the Optional value in this code `private func updateUI() { interstTitleLabel.text = interest2.title futerdimageview.image = interest2.featuredImage! }` the error come when i put your code – faten Sep 22 '17 at 12:11
  • Let take this to [stackoverflow chat](https://chat.stackoverflow.com/rooms/155081/swift-withreuseidentifier) – chirag90 Sep 22 '17 at 12:21
  • You must have 20 reputation on Stack Overflow to talk here :( did you see the video ? i will add some code for another tow class – faten Sep 22 '17 at 12:23
  • Sorry dude, i am not watching 35 minutes of video. Like i mention put some break point in in your updateUI() function and step over it. to see what is coming out as nil. – chirag90 Sep 22 '17 at 12:25