如何在一个表格中显示多行查看单元格
问题描述:
我有一个稍长的字符串,在tablView单元格中显示时会被截断。我这样做是为了使表格视图变宽:如何在一个表格中显示多行查看单元格
tableView.rowHeight = 100;
如何使字体变小以及将表格视图单元格中的文字换行?
答
在tableView:cellForRowAtIndexPath:
中,您可以在textLabel(或descriptionLabel,取决于您使用的单元格样式)上设置一些属性来执行此操作。设置font
更改字体,linkBreakMode
使其自动换行,并numberOfLines
设置多少行最大(在该点之后截断它,你可以设置为0则没有最大
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* aCell = [tableView dequeueReusableCellWithIdentifier:kMyCellID];
if(aCell == nil) {
aCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kMyCellID] autorelease];
aCell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:10.0];
aCell.textLabel.lineBreakMode = NSLineBreakByWordWrapping; // Pre-iOS6 use UILineBreakModeWordWrap
aCell.textLabel.numberOfLines = 2; // 0 means no max.
}
// ... Your other cell setup stuff here
return aCell;
}
答
你应该子类UITableViewCell,并使其具有UITextView而不是UITextField。然后将你的子类分配给你的UITableView。
答
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* Cell = [tableView dequeueReusableCellWithIdentifier:kMyCellID];
if(Cell == nil) {
Cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kMyCellID] autorelease];
Cell.textLabel.font = [UIFont fontWithName:@"TimesNewRoman" size:10.0];
Cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
Cell.textLabel.numberOfLines = 2; // 0 means no max.
}
// your code here
return Cell;
}
这在iOS6中不推荐使用。你如何在iOS6中使用??? – Napolux 2012-09-27 20:50:13
据我所知,iOS6中唯一弃用的是'UILineBreakModeWordWrap','NSLineBreakByWordWrapping'是iOS6的等价物,更新了我的答案。 – zpasternack 2012-09-27 21:16:02