2009-08-05 21 views
9

Tôi dường như không thể nhận được văn bản thực sự trải rộng trên nhiều dòng. Chiều cao nhìn chính xác. Tôi đang thiếu gì?Làm cách nào để tạo UITableViewCell có kích thước thay đổi?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"StatusCell"] autorelease]; 

    CGRect frame = cell.contentView.bounds; 
    UILabel *myLabel = [[UILabel alloc] initWithFrame:frame]; 
    myLabel.text = [[person.updates objectAtIndex:indexPath.row] valueForKey:@"text"]; 
    [cell.contentView addSubview:myLabel]; 

    [myLabel release]; 

    return cell; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    NSString *text = [[person.updates objectAtIndex:indexPath.row] valueForKey:@"text"]; 
    UIFont *font = [UIFont systemFontOfSize:[UIFont systemFontSize]]; 
    CGSize withinSize = CGSizeMake(tableView.frame.size.width, 1000); 
    CGSize size = [text sizeWithFont:font constrainedToSize:withinSize lineBreakMode:UILineBreakModeWordWrap]; 

    return size.height + 20;  
} 

Ngoài ra, tôi thiếu gì khiến nhãn xuất hiện lâu hơn ô bảng? alt text http://i31.tinypic.com/15chnjd.gif

Trả lời

15

Tweetero cung cấp ví dụ về điều này trong MessageListController.m. Mã này có làm cho cửa sổ sau:

http://ec.mashable.com/wp-content/uploads/2009/04/imageshackscreen.jpg

(Pic được lấy từ Mashable).

Khái quát thực hiện cơ bản:

  1. Khi xây dựng một UITableViewCell, tạo và thêm một UILabel như là một subview trong cách thể hiện trong tableviewCellWithReuseIdentifier:. Tìm kiếm nhãn hiệu TEXT_TAG.

  2. khi làm phong phú thêm UITableViewCell với chế độ xem, đảm bảo rằng bạn định dạng nhãn đúng cách, như được thực hiện trong configureCell:forIndexPath, tương tự, hãy tìm nhãn thẻ TEXT_TAG.

  3. Trả về chiều cao thích hợp cho từng ô, như được thực hiện trong tableView:heightForRowAtIndexPath.

+0

Điều này thật tuyệt. Cảm ơn. –

2
myLabel.numberOfLines = 2; 

Kiểm tra docs để biết đầy đủ về cách sử dụng tài sản này.

+0

vấn đề là nó sẽ không luôn luôn là một con số cụ thể, nó sẽ thay đổi cho mỗi hàng – Jason

+0

Thuộc tính numberOfLines đặt ** tối đa ** số dòng. Đặt giá trị cao. (Và, như Rob đã nói, hãy kiểm tra tài liệu.) –

+2

Bạn cũng phải đặt myLabel.lineBrakeMode thành UILineBreakModeWordWrap. –

0
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return theSizeYouWantYourCellToBe; 
} 
Các vấn đề liên quan