2010-09-23 80 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; 
} 
相關問題