简体   繁体   中英

How to resize UITableViewCell after setting its size in HeightForRowAt func in Swift?

I want the latest TableViewCell, to be as big as the TableView, but I want the old cells to return to normal size, everytime I create a new one.

I've already tried, doing it in the HeightForRowAt function, but every cell remains on the TableView size.

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    var cellHeight: CGFloat = CGFloat()
    for cell in tableView.visibleCells {
        cellHeight = cell.bounds.height
    }

    let TableHeight = tableView.frame.height


    if indexPath.row == arr.count - 1 {
        return TableHeight
    }else{

        return cellHeight
    }

}

Is there any way to resize the cell after its creation, or any other solution to my problem?

Thanks in advance

Just try to put constant number in a normal cell height

 if indexPath.row == arr.count - 1 {
    return TableHeight
 }else{
    return 40 // or whatever you want
 }

if you ask your self why? First time your cells height will be zero and the last one will be TableHeight

Then each time heightForRowAt method called, your cells heights will be = TableHeight and last one height also will be TableHeight .

because your for loop will return the last cell height from visibleCells and you have one visible cell "other cells not visibles height = zero" , so it will be TableHeight

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM