簡體   English   中英

根據1個標簽動態更改UITableViewCell的高度?

[英]Change the height dynamically on UITableViewCell's based on 1 label?

如何根據UITableViewCells中的內容量縮放它們? 在我的單元格中,我使用3個代表論壇的標簽。 標簽被命名為“別名”,“日期”和“評論”。 第三個標簽注釋可以是任意數量的行。 因此,我的單元格需要根據“注釋”標簽中文本(大小)的數量動態變化。 以下是我的相關代碼:

- (UITableViewCell *)tableView:(UITableView *)pTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ForumthreadCell";
UITableViewCell *cell = [pTableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

Feedback *item = [self.items objectAtIndex:indexPath.row];

UILabel *aliasLabel = (UILabel *)[cell viewWithTag:1];
UILabel *commentLabel = (UILabel *)[cell viewWithTag:2];
UILabel *dateLabel = (UILabel *)[cell viewWithTag:3];

[aliasLabel setText:item.alias];
[commentLabel setText:item.comment];
[dateLabel setText:[self.dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:(double)item.time]]];

commentLabel.numberOfLines = 0;
[commentLabel sizeToFit];

return cell;
}

在這種方法中,我應該根據commentLabel的高度設置tableViewCell的高度。唯一的問題是我不確定該怎么做?

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

    return ????;
    //return [indexPath row] + 50;

}

return ????;
}

我需要某人的幫助,這些人試圖看一下我的代碼並舉例說明如何進行這項工作。 畢竟,它只有1個標簽。...我已經看過幾本教程,但無法使其正常工作...感謝您的時間和幫助。 這是帶有一些舊代碼的舊屏幕。 這或多或少是該論壇現在的樣子: http : //tinypic.com/view.php?pic=16h7me9&s=6

您需要的是一種計算使用特定字體呈現的文本大小的方法。 幸運的是,UIKit通過NSString UIKit Additions中的各個sizeWithFont:方法為您提供了這一點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM