细胞高度不正常

时间:2014-07-07 09:31:15

标签: ios iphone objective-c

我使用以下代码动态更改单元格高度。但它没有显示任何东西。谁能帮助我做到这一点。

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(!self.customCell){
        self.customCell = [self.tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
    }

    //Cell configuration
    self.customCell.title.text = vendor[indexPath.row];

    int quoteIndex = indexPath.row % [vendor count];
    self.customCell.description.text = message[quoteIndex];

    //Cell Layout
    [self.customCell layoutIfNeeded];

    //Height of cell
    CGFloat height = [self.customCell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    //    CGFloat height = 240;

    return height;
}

2 个答案:

答案 0 :(得分:1)

在自定义单元格中,描述标签 - 设置行数= 0

[Label SizeToFit] 会对您有所帮助。
此代码可能对您有所帮助。

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(!self.customCell){
        self.customCell = [self.tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
    }

    //Cell configuration
    self.customCell.title.text = vendor[indexPath.row];

    int quoteIndex = indexPath.row % [vendor count];
    self.customCell.description.text = message[quoteIndex];

    [self.customCell.description sizeToFit];

    float height = (CGRectGetMaxY(self.customCell.description.frame) + 5);
    return height;
}

由于

答案 1 :(得分:0)

您使用相同的引用来引用单元格,但您必须在内部创建单独的单元格引用,如下所示:

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
     static CustomCell *customCell = nil;
     static dispatch_once_t onceToken;
    dispatch_once(&onceToken, {
           customCell = [self.tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
        });

// Rest of your code

    }