2010-09-23 116 views

回答

14

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; 
} 
+0

这在iOS6中不推荐使用。你如何在iOS6中使用??? – Napolux 2012-09-27 20:50:13

+1

据我所知,iOS6中唯一弃用的是'UILineBreakModeWordWrap','NSLineBreakByWordWrapping'是iOS6的等价物,更新了我的答案。 – zpasternack 2012-09-27 21:16:02

0

你应该子类UITableViewCell,并使其具有UITextView而不是UITextField。然后将你的子类分配给你的UITableView。

2
- (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; 
}