【发布时间】:2008-09-24 15:54:26
【问题描述】:
我想创建一个具有不同行高的 UITableView,我正在尝试通过在 UITableViewCells 中创建 UILabel 来实现这一点。
到目前为止,这是我的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"EntryCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
}
UILabel *textView = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 300, 40)];
textView.numberOfLines = 0;
textView.text = [entries objectAtIndex:[indexPath row]];
[cell.contentView addSubview:textView];
[textView release];
return cell;
}
这给了我每个单元格 2 行文本。但是,每个“条目”都有不同的行数,我希望 UITableViewCells 自动调整大小,根据需要包装文本,而不更改字体大小。
[textView sizeToFit] 和/或[cell sizeToFit] 似乎不起作用。
这是我希望 UITableView 的外观:
----------------
Lorem ipsum
----------------
Lorem ipsum
Lorem ipsum
----------------
Lorem ipsum
Lorem ipsum
Lorem ipsum
----------------
Lorem ipsum
----------------
Lorem ipsum
Lorem ipsum
----------------
有人知道如何正确执行此操作吗?
谢谢。
【问题讨论】:
标签: iphone cocoa-touch uitableview user-interface