Custom UITableViewCell heightForRowAt not work in Swift 4

2775 views ios
5

I created a custom UITableView from nib and am trying to change the row height in the Size Inspector in ShopImagesViewCell.

ShopImagesViewCell.xib :

enter image description here

So I wrote the code to change the row height in ShopImagesViewCell.swift.

class ShopImagesViewCell: UITableViewCell, UITableViewDelegate
{
    @IBOutlet weak var imageCollectionView: UICollectionView!
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var starView: UIView!
    @IBOutlet weak var starImageView: UIImageView!

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

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
    { // This function does not work.
        return CGFloat(300) // 300 changes fluidly
    }
}

extension ShopImagesViewCell
{
    func setView()
    {
        self.imageCollectionView.delegate = self
        self.imageCollectionView.dataSource = self
        self.imageCollectionView.regCells(cells: ["ShopImageCollectionViewCell","ShopNonImageCollectionViewCell"])
    }
}

Since the function does not work, I tried to find a way and tried to do

self.tableView.delegate = self

However, tableView did not exist and I tried to add ShopImagesViewCell using @outlet, but it was not possible.

How do I make the above function work?

If you want to comment on additional source code, I will add it if possible.

answered question

1 Answer

0

Your cell is a UITableViewCell that contains a UICollectionView. Therefore your cell class should conform to the UICollectionViewDataSource and UICollectionViewDelegate` methods. Your table view cell should not implement any table view data source or delegate methods nor conform to the table view protocols. Put those in the view controller with the table view.

posted this

Have an answer?

JD

Please login first before posting an answer.