简体   繁体   中英

Varying length cells in TableView make the view snap while scrolling Swift

So I have a TableView with 2 prototype cells. Here is my code:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        if indexPath.row == 0 {

            let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! ItemDetailsCell

            cell.centerImage.image = mainImage

            cell.heading.text = detailsHeadings[indexPath.row]
            let headingString = detailsHeadings[indexPath.row]
            cell.body.text = details[headingString]

            tableView.rowHeight = cell.labelBlack.frame.height + 40

            return cell

        } else {

            let cell = tableView.dequeueReusableCellWithIdentifier("Cell2", forIndexPath: indexPath) as! ItemDetailsCell

            cell.heading.text = detailsHeadings[indexPath.row]
            let headingString = detailsHeadings[indexPath.row]
            cell.body.text = details[headingString]
            let bodyString = details[headingString]

            let labelWidth = Int(cell.body.frame.width)
            println(labelWidth)
            let label = UILabel(frame: CGRect(x: 0, y: 0, width: labelWidth, height: 10000))
            label.text = bodyString
            label.numberOfLines = 100
            label.font = UIFont(name: "OpenSans-Light", size: 12.0)
            label.sizeToFit()
            tableView.rowHeight = label.frame.height + 3

            return cell

        }

    }

So the second prototype cell has just two labels with the values being assigned from a Dictionary. The cell size needs to expand or contract based upon how many lines of text there are. In auto layout I have the number of lines set to 0 so it will pick however many lines are needed. This works fine except when you scroll within the app it will snap the view up as users scroll back up from the bottom. Is there a way to avoid this?

Thanks in advance for any help.

I found this actually after spending some more time looking:

http://candycode.io/automatically-resizing-uitableviewcells-with-dynamic-text-height-using-auto-layout/

It gave me what I needed. I removed the parts of my code that set the rowHeight and then used the viewDidLoad method as well as auto layout to constrain my cell sizes and after a bit of trial and error it is working without snapping to place when you scroll.

You are changing the rowHeight of the tableview. That's most likely very wrong. The rowHeight of the tableView is the default for all rows that don't have their own height, so you are effectively changing the height of all cells.

You should use the delegate method tableView:heightForRowAtIndexPath:

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