我試圖讓UITableView單元格適合文本。到目前爲止,我有這樣的事情:ios - 無法獲得與文本數量成正比的UITableView單元格高度
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UILabel *label = nil;
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault)
reuseIdentifier:@"business"];
NSString *comment = [[items_array objectAtIndex:[indexPath row]] objectForKey:(@"comment")];
cell.textLabel.numberOfLines = 0;
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
CGSize constraint = CGSizeMake(320 - (10 * 2), 20000.0f);
CGSize size = [comment sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setText:comment];
[label setFrame:CGRectMake(10, 10, 320 - (10 * 2), MAX(size.height, 44.0f))];
cell.textLabel.text = comment;
}
我也有這個功能
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSString *comment = [[items_array objectAtIndex:[indexPath row]] objectForKey:(@"comment")];
CGSize constraint = CGSizeMake(320 - (10 * 2), 20000.0f);
CGSize size = [comment sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(size.height, 44.0f);
return height + (10 * 2);
}
但它不是高度適量分配給UITableViews的細胞。我想我在分配標籤和單元格文本的方式/方式上犯了一些錯誤。
請幫我理解它應該如何。
謝謝!
我看到你在你的cellForRowAtIndexPath中製作一個UILabel,但只是將cell.textLabel設置爲你的文本。即您不將此UILabel添加到任何現有視圖。你有沒有嘗試過在cell.contentView上做setFrame? – CSmith 2012-08-07 18:33:21
@CSmith不,我沒有嘗試過。你是對的,在那裏我有我的困惑...準確地在哪些使用textLabel或UILabel ....以及如何設置它的權利。 – GeekedOut 2012-08-07 18:41:08
我發佈了一個答案,希望能讓你更接近... – CSmith 2012-08-07 18:46:36