如何更改分组tableViewCell的高度?
问题描述:
我正在尝试显示其在iPhone的联系人应用程序中显示的地址。所以看起来我需要一个三倍于普通细胞的细胞。因为它总是相同的,我只需要在代码中应用固定的高度。但我不知道该怎么做。我当前的代码是如何更改分组tableViewCell的高度?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *address = [NSString stringWithFormat:@"%@\n"@"%@ %@ %@\n"@"%@", dispAddress, dispCity, dispState, dispZip, dispCountry];
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.detailTextLabel.numberOfLines = 3; // 0 means no max.
cell.detailTextLabel.text = address;
return cell;
}
答
要设置的所有细胞相同的固定高度,在viewDidLoad中或其他一些适当的地方,这样做:
self.tableView.rowHeight = 100;
要设置单元基于索引路径不同的高度,使用heightForRowAtIndexPath委托方法:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ((indexPath.section == 0) && (indexPath.row == 0))
return 100;
else
return 44;
}
非常感谢安娜,heightForRowAtIndexPath工作完美。非常感谢。 – Hitz 2011-04-26 05:18:24