如何让UITableViewController在两个不同的高度显示两个不同的UITableView单元?

时间:2012-05-14 22:28:36

标签: ios ios5 uitableview

我有一个UITableView控制器,UITableView xib和两个带有xib的UITableViewCell子类。我为每个xib拖出了一个单独的单元格。我的第一个单元格高度为115,而我的另一个单元格具有默认高度。当我的控制器中调用cellForRowAtIndexPath时,我有一个条件语句,将第一行设置为更大的单元格。之后的每一行都设置为我的其他单元格。即使认为正在渲染更大的单元格,默认单元格也没有意识到第一个单元格大于默认单元格的事实,因此默认单元格与更大的单元格重叠。我在我的大单元底部设置了一个distanceLabel插座,以显示我的意思:

enter image description here

注意: 2.3英里是第一个单元格的一部分。

- (void)viewDidLoad
{
    [super viewDidLoad];

    // load the cell nib
    UINib *nib = [UINib nibWithNibName:@"CustomViewCell" bundle:nil];
    UINib *bigNib = [UINib nibWithNibName:@"BigCell" bundle:nil];

    //Register the nib
    [[self tableView] registerNib:nib forCellReuseIdentifier:@"CustomViewCell"];
    [[self tableView] registerNib:bigNib forCellReuseIdentifier:@"BigCell"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        if (indexPath.row == 0) {
        BigCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BigCell"];
        NSString *title = [self.frontList objectAtIndex:indexPath.row];
        [[cell nameLabel] setText:title];
        [[cell distanceLabel] setText:@"2.3 miles"];
        return cell;
    } else {

        CustomFrontCell *cell  = [tableView dequeueReusableCellWithIdentifier:@"CustomViewCell"];
        NSString *titles = [self.frontList objectAtIndex:indexPath.row];
        [[cell nameLabel] setText:titles];
        return cell;
    }
}

如何使用正确的间距和高度来渲染这些单元格?

1 个答案:

答案 0 :(得分:2)

您可以使用UITableViewDelegate方法tableView:(UITableView *)tableView heightForRowAtIndexPath:来控制身高。

相关问题