Buster
Buster

Reputation: 697

UITableView Row Heigh Changing Between Scenes (swift)

I have a tableview set up to load from an array. I want the first item to show in the table as the "user's profile" so the height is set differently from the other cells like so:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    //some conditional to see which cell we use

    //if it's the first cell make it the profile cell - basic cell
    if (indexPath.row==0) {
        //set the height
        self.tableView.rowHeight = 230.0

        //call the profile cell
        return basicCellAtIndexPath(indexPath)

    } else //do they have friends?
        {
        //set the height
        self.tableView.rowHeight = 70.0

        //get the friend var
        let item: String = TableDataFriends[indexPath.row]

        if(item=="nofriends") {
            //no friends. Tell them to invite some
            return inviteCellAtIndexPath(indexPath)
        } else {
            //got friends. Show them with the friend cell with image
            return imageCellAtIndexPath(indexPath)
        }
    }
}

It works fine when the page loads initially, however when I navigate from the table page to any other scene and then back, all cell heights are 70 and the "profile cell" is now merged layered behind the other cells. I've tried a few different things to ensure that the height is set up properly. The below two functions I tried did nothing.

//test for table height
override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    if(indexPath==1) {
        return 230.0
    } else {
        return 70.0
    }

}


//test for table height
func configureTableView() {
    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight = 230.0
}

How do I make sure the first cell's height stays at 230 and the others remain at 70 between page navigation? Thanks!

Upvotes: 0

Views: 78

Answers (3)

Buster
Buster

Reputation: 697

All I had to do is add the following and remove the rowHeight.

override func tableView(tableView: UITableView, heightForRowAtIndexPath     indexPath: NSIndexPath) -> CGFloat {
    let ix = indexPath.row

    if ix == 0 {
        return 230.0
    }
    else {
        return 70.0
    }

}

Upvotes: 0

Kevin Owens
Kevin Owens

Reputation: 548

I have implemented variable row heights by overriding the UITableViewDelegate method tableView(_:heightForRowAtIndexPath:). You were on the right track with tableView(_:estimatedHeightForRowAtIndexPath:).

A note about what you attempted in your tableView(_:cellForRowAtIndexPath:) override: The rowHeight property applies to all rows in the tableView, so while you think you're changing it for one row, you're actually changing it for all of them.

Upvotes: 1

neo D1
neo D1

Reputation: 1710

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if(indexPath==0) {
            return 230.0
        } 
    else {
            return UITableViewAutomaticDimension 
     }

   }

You need not specify anything related to height in cellForRowAtIndexPath .

Upvotes: 0

Related Questions