以编程方式为单个UITableViewCell设置高度?

Jos*_*ane 11 iphone objective-c uitableview

我需要以编程方式在UITableView中设置单个UITableViewCell的高度.我怎样才能做到这一点?

我需要这个单元格高150像素,所有其他单元格可以保持默认的44像素高度.

谢谢.

Max*_*Max 47

UITableViewCell高度有一个委托函数.

在这里,您指定该特定单元格的indexPath并返回它的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.section == yourSection && indexPath.row == yourRow) {
        return 150.0;
    }
    // "Else"
    return someDefaultHeight;
}
Run Code Online (Sandbox Code Playgroud)


Lou*_*uie 7

你必须知道,或弄清楚你想要制作更高的单元格索引.让我们说你的第一个细胞.

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row == 0) { //change 0 to whatever cell index you want taller
        return 150;
    }
    else {
        return 44;
    }   
}
Run Code Online (Sandbox Code Playgroud)


Nic*_*k89 5

SWIFT答案

在方法中指定要更改的部分和行(请记住计数从0开始,而不是1)heightForRowAtIndexPath.

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat{
    if indexPath.section == 0 && indexPath.row == 0{
        return 150
    }
    return 44.0
}
Run Code Online (Sandbox Code Playgroud)