2010-09-23 34 views
5

Tôi có một chuỗi hơi dài bị cắt bớt khi được hiển thị trong ô tablView. Tôi đã làm điều này để làm cho chế độ xem bảng rộng hơn:Cách hiển thị nhiều dòng trong ô tableView

tableView.rowHeight = 100; 

Làm cách nào để làm cho phông chữ nhỏ hơn cũng như bọc văn bản trong ô xem bảng?

Trả lời

14

Trong tableView:cellForRowAtIndexPath: bạn có thể đặt một vài thuộc tính trên textLabel (hoặc descriptionLabel, tùy thuộc vào kiểu ô bạn đang sử dụng) để thực hiện việc này. Đặt font để thay đổi font chữ, linkBreakMode để làm cho nó word-wrap, và numberOfLines để thiết lập có bao nhiêu dòng max (sau đó trỏ nó truncates. Bạn có thể đặt đó để 0 cho không max.

- (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

này không được chấp nhận trong iOS6. Làm thế nào để bạn có thể làm cho nó trong iOS6? – Napolux

+1

Theo tôi biết điều duy nhất không được chấp nhận trong iOS6 là 'UILineBreakModeWordWrap'.' NSLineBreakByWordWrapping' là tương đương iOS6. – zpasternack

0

Bạn nên phân lớp UITableViewCell và làm cho nó có UITextView thay vì UITextField. Sau đó gán phân lớp của bạn cho UITableView của bạn.

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; 
} 
Các vấn đề liên quan