Home > Back-end >  How to show content from second Cell in Swift?
How to show content from second Cell in Swift?

Time:12-07

i want to make Swift app with CollectionView in Xcode, i have implemented it well and now i want to show some other stuff after some Posts, and i have created Xib and swift file for it. but the problem is when i try to show label in new cell i got this error ,,

enter image description here

i have registered new cell in ViewDidLoad Like this:

 collectionView.dataSource = self
    collectionView.delegate = self
    collectionView.collectionViewLayout = UICollectionViewFlowLayout()
    collectionView.register(AdViewCell.self, forCellWithReuseIdentifier: "adCell")

and i have used 300X250 space in AdViewCell and when i load posts i can see the white space of 300X250 size.. look at this

enter image description here

but even when i try to change its background color or anything , nothing happens when app is loaded..

i have connected Label to AdViewCell also..

class AdViewCell: UICollectionViewCell {

@IBOutlet weak var namelbl: UILabel!


override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

please help

CodePudding user response:

Your are using "cell" and "adcell" for the same indexPath.

try this:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
    if indexPath.item % 5 == 1 {

        let adcell = collectionView.dequeueReusableCell(withReuseIdentifier: "adcell", for: indexPath) as! AdViewCell

        return adcell
    }else{

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MovieCollectionViewCell", for: indexPath) as! MovieCollectionViewCell

        return cell 
    }
}

CodePudding user response:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

let indexPath = tableView.indexPathForSelectedRow() //optional, to get from any UIButton for example

let currentCell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell

print(currentCell.textLabel!.text)
  • Related