我需要以编程方式在我的UITableView中为单个UITableViewCell设置高度。我怎样才能做到这一点?以编程方式为单个UITableViewCell设置高度?
我需要这一个单元格为150像素高,其他所有可以保持其默认的44像素的高度。
谢谢。
我需要以编程方式在我的UITableView中为单个UITableViewCell设置高度。我怎样才能做到这一点?以编程方式为单个UITableViewCell设置高度?
我需要这一个单元格为150像素高,其他所有可以保持其默认的44像素的高度。
谢谢。
有一个UITableViewCell高度的委托函数。
在这里您可以指定特定细胞的indexPath,回到你的高度,它
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.section == yourSection && indexPath.row == yourRow) {
return 150.0;
}
// "Else"
return someDefaultHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
您可以使用此方法为特定索引路径设置单元格的值,并为其他单元格设置默认值。
你必须知道,或者找出你想高大的一个什么细胞指数。 可以说它是你的第一个细胞。
-(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;
}
}
SWIFT ANSWER
指定哪些部分和行要更改(记住计数从0开始,而不是1)在heightForRowAtIndexPath
方法。
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat{
if indexPath.section == 0 && indexPath.row == 0{
return 150
}
return 44.0
}