Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
494 views
in Technique[技术] by (71.8m points)

ios - Changing height of individual cell UITableView

I would like something like the following:

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

    let cell = tableView.dequeueReusableCellWithIdentifier("NewsCell", forIndexPath: indexPath) as! UITableViewCell

    if indexPath == 3{

        cell.height = "50dp"

    }       

    return cell

}

what is the alternative or the easiest way to go about this?

EDIT

Is it possible for me to also specify section number: i.e-

if sectionIndex == 5
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I suggest use the heightForRowAtIndexPath function. TableView will call this function to determine the row height for a specific indexpath.

Sample code:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if indexPath.section == SECTION_INDEX {
        return 60
    } else {
        return UITableViewAutomaticDimension
    }
}

Swift 4:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if indexPath.section == SECTION_INDEX {
        return 60
    } else {
        return UITableViewAutomaticDimension
    }
}

A little explain: indexPath have two properties: section and row. By checking these two properties, you can make your own control flow to determine the height for each cell.

If you return 60, it means you want the cell's height to be 60 points. If you return UITableViewAutomaticDimension, you want the system to decide the best height for you (in this case, you'd better set autolayout for the cell in storyboard).

I also suggest you take the stanford course CS193p on iTunes U, it would be of great help to you :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...