[英]iOS - Auto resize cell height & label height
我是iOS編程的新手,正在開發一個具有表格視圖的應用程序。
表數據由plist文件加載。 桌子的一行很長,而且不一致,因此我需要動態高度。
這是我到目前為止的內容:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellText = @"This is a very long peice of text to show up here and here";// this will eventually be data from a plist
UIFont *cellFont = [UIFont fontWithName:@"Helvetica-neuve" size:21.0];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
int buffer = 70;
return labelSize.height + buffer+100;
//return 65;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CustomTableCell";
static NSString *CellNib = @"DetailViewCell";
DetailViewCell *cell = (DetailViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
cell = (DetailViewCell *)[nib objectAtIndex:0];
cell.cellSubtitleLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.cellSubtitleLabel.numberOfLines = 0;
cell.cellSubtitleLabel.font = [UIFont fontWithName:@"Helvetica-neuve" size:21.0];
}
cell.accessoryType = UITableViewCellAccessoryNone;
informations = [[NSArray alloc] initWithObjects:@"Title", @"Country", @"State", @"Population", @"Info", nil];
subtitles = [[NSArray alloc] initWithObjects:titleString, subtitleString, stateString, populationString, @"This is a very long peice of text to show up here and here", nil];
cell.cellTitleLabel.text = [informations objectAtIndex:indexPath.row];
cell.cellSubtitleLabel.text = [subtitles objectAtIndex:indexPath.row];
return (DetailViewCell *) cell;
}
我設法使它達到了可以確定的高度。 但是標簽文字沒有顯示。 我已經縮小了標簽的高度。 現在是23點(在Interface Builder中設置)。 如果我增加尺寸,則會弄亂其他單元格。 我需要根據其內容更改標簽的高度。
有人可以幫我這個忙,因為我花了兩天的時間反復嘗試,但仍然沒有做到。 我也是這個菜鳥,所以請詳細解釋;)
非常感謝
瑞安
我目前正在做同樣的事情。 這就是我的工作。
使用sizeToFit
方法在cellForRowAtIndexPath
回調中調整UILabel的大小:
[contentViewCell.myTextLabel sizeToFit];
希望這會幫助你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.