我似乎无法让文本真正跨越多行。高度看起来正确。我错过了什么?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"StatusCell"] autorelease];
CGRect frame = cell.contentView.bounds;
UILabel *myLabel = [[UILabel alloc] initWithFrame:frame];
myLabel.text = [[person.updates objectAtIndex:indexPath.row] valueForKey:@"text"];
[cell.contentView addSubview:myLabel];
[myLabel release];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *text = [[person.updates objectAtIndex:indexPath.row] valueForKey:@"text"];
UIFont *font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
CGSize withinSize = CGSizeMake(tableView.frame.size.width, 1000);
CGSize size = [text sizeWithFont:font constrainedToSize:withinSize lineBreakMode:UILineBreakModeWordWrap];
return size.height + 20;
}
最佳答案
Tweetero
在 MessageListController.m
中提供了一个例子.那里的代码呈现以下屏幕:
(图片来自Mashable)
基本实现大纲:
在构建
UITableViewCell
时,按照tableviewCellWithReuseIdentifier:
中所示的方式创建并添加一个UILabel
作为 subview 。寻找TEXT_TAG
标签的创建。当使用 View 丰富
UITableViewCell
时,请确保正确格式化标签,就像在configureCell:forIndexPath
中所做的那样,类似地查找标签标签TEXT_TAG
。返回每个单元格的适当高度,如
tableView:heightForRowAtIndexPath
中所做的那样。
关于iphone - 如何创建可变大小的 UITableViewCell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1233608/