Klemen Zagar Klemen Zagar - 10 days ago 7
Swift Question

Is it posible for UITableView to include some self sized cells and custom height cells?

I have I working

UITableView
with self sized cells (
UITableViewAutomaticDimension
) with news content. And this works as it should :)

And now I come to a part where I have to add one cell in the middle of the news cells, and its height should be the same size as the device screen.

I tried with adding this height method from
UITableView
class:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let item = feed[indexPath.row]
if item is News {
return UITableViewAutomaticDimension
} else if item is ConfigurationBit {
return view.bounds.size.height
}
return 0.0
}


but this don't give correct result. Do you have any suggestions?

UPDATE
The following code is correct. The issue was elsewhere and it was not relevant to this case.

Answer

I think you are missing to set rowHeight and estimatedRowHeight for tableview .Please find code i have used

var arrayOfCellData = [Any]()

    override func viewDidLoad() {
        super.viewDidLoad()
        arrayOfCellData = [cellData(cell : 1, text : "asdadas", image : #imageLiteral(resourceName: "mark")),
                           cellconfigData(cell : 2, text : "dasdadad", image : #imageLiteral(resourceName: "mark")),
                           cellData(cell : 3, text : "asraerqsadas", image : #imageLiteral(resourceName: "mark")),
                           cellconfigData(cell : 4, text : "asraerqsadas", image : #imageLiteral(resourceName: "mark")),
                           cellData(cell : 5, text : "asraerqsadas", image : #imageLiteral(resourceName: "mark")),
                           cellconfigData(cell : 6, text : "asraerqsadas", image : #imageLiteral(resourceName: "mark"))]
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 245
    }


    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arrayOfCellData.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let item = arrayOfCellData[indexPath.row]
        if item is cellData{
            let cell = Bundle.main.loadNibNamed("OwnTableViewCell", owner: self, options: nil)?.first as! OwnTableViewCell

            cell.mainImageView.image = (arrayOfCellData[indexPath.row] as! cellData).image
            cell.mainLabel.text = (arrayOfCellData[indexPath.row] as! cellData).text
            return cell
        }
        else
        {
            let cell = Bundle.main.loadNibNamed("NewTableViewCell", owner: self, options: nil)?.first as! NewTableViewCell

            cell.mainImageView.image = (arrayOfCellData[indexPath.row] as! cellconfigData).image
            cell.mainLabel.text = (arrayOfCellData[indexPath.row] as! cellconfigData).text

            return cell
        }
    }

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let item = arrayOfCellData[indexPath.row]

        if item is cellData{
            return UITableViewAutomaticDimension
        }
        else if item is cellconfigData{
            return 100
        }
        return 0.0
    }
Comments