2012-08-07 29 views
0

我試圖讓UITableView單元格適合文本。到目前爲止,我有這樣的事情:ios - 無法獲得與文本數量成正比的UITableView單元格高度

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UILabel *label = nil; 

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) 
      reuseIdentifier:@"business"]; 

    NSString *comment = [[items_array objectAtIndex:[indexPath row]] objectForKey:(@"comment")]; 

cell.textLabel.numberOfLines = 0; 
     cell.textLabel.lineBreakMode = UILineBreakModeWordWrap; 

     CGSize constraint = CGSizeMake(320 - (10 * 2), 20000.0f); 

     CGSize size = [comment sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 


     label = [[UILabel alloc] initWithFrame:CGRectZero]; 

     [label setText:comment]; 
     [label setFrame:CGRectMake(10, 10, 320 - (10 * 2), MAX(size.height, 44.0f))]; 

     cell.textLabel.text = comment; 
    } 

我也有這個功能

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; 
{  
    NSString *comment = [[items_array objectAtIndex:[indexPath row]] objectForKey:(@"comment")]; 

    CGSize constraint = CGSizeMake(320 - (10 * 2), 20000.0f); 

    CGSize size = [comment sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 

    CGFloat height = MAX(size.height, 44.0f); 

    return height + (10 * 2); 
} 

但它不是高度適量分配給UITableViews的細胞。我想我在分配標籤和單元格文本的方式/方式上犯了一些錯誤。

請幫我理解它應該如何。

謝謝!

+0

我看到你在你的cellForRowAtIndexPath中製作一個UILabel,但只是將cell.textLabel設置爲你的文本。即您不將此UILabel添加到任何現有視圖。你有沒有嘗試過在cell.contentView上做setFrame? – CSmith 2012-08-07 18:33:21

+0

@CSmith不,我沒有嘗試過。你是對的,在那裏我有我的困惑...準確地在哪些使用textLabel或UILabel ....以及如何設置它的權利。 – GeekedOut 2012-08-07 18:41:08

+0

我發佈了一個答案,希望能讓你更接近... – CSmith 2012-08-07 18:46:36

回答

1

這是否讓你更接近?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"business"]; 

if (cell == nil) 
{ 
    cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault)reuseIdentifier:@"business"]; 
} 

NSString *comment = [[items_array objectAtIndex:[indexPath row]] objectForKey:(@"comment")]; 

CGSize constraint = CGSizeMake(320 - (10 * 2), 20000.0f); 

CGSize size = [comment sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, MAX(size.height, 44.0f) + 20.0f)]; 
label.numberOfLines = 0; 
label.lineBreakMode = UILineBreakModeWordWrap; 
label.text = comment; 

[cell.contentView addSubview:label]; 

} 
+0

@cssmith現在試試這個 - 謝謝:) – GeekedOut 2012-08-07 18:47:45

+0

@cssmith它的工作原理...不得不多花點時間。你的代碼工作得很好。 – GeekedOut 2012-08-07 18:51:53

+0

@cssmith我剛剛測試了一個較長的字符串,其中一些不適合。這可能是最大高度的問題嗎?有沒有辦法對最大高度沒有限制? – GeekedOut 2012-08-07 19:09:23

1

您需要使用-tableView:heightForRowAtIndexPath來設置單元格的高度。


有了新的代碼發佈

這裏是我做到了(我用了一個原型細胞這樣的事情可能對你的不同)。

我將單元格的自動大小設置爲自動調整標籤高度(這可能已經爲textLabel設置)。

然後在-tableView:heightForRowAtIndexPath我做了以下內容:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; 
{  
    CGSize size = CGSizeMake(WIDTH_OF_PROTOTYPE_LABEL, CGFLOAT_MAX); 
    size = [TEXT_AT_INDEX_PATH sizeWithFont:FONT_OF_PROTOTYPE_LABEL constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap]; 

    CGFloat height = size.height + (HEIGHT_OF_PROTOTYPE_CELL - HEIGHT_OF_PROTOTYPE_LABEL); 
    return MAX(height, HEIGHT_OF_PROTOTYPE_CELL); 
} 

更新2

在看你的代碼,我再次看到兩個可能的問題。 1)textLabel的寬度有誤。 2)在計算最終的新高度之後,您應該計算do MAX作爲最後一件事。

+0

我剛剛更新了我的問題。我有這個功能,但不知道如何/在哪裏使它們一起工作。此外,大部分函數的代碼都在我的原始函數中。 – GeekedOut 2012-08-07 18:26:33

+0

感謝您的幫助。另一個答案奏效,但你也讓我提前瞭解正在發生的事情 - 非常感謝。 – GeekedOut 2012-08-07 18:55:09

相關問題