2013-03-26 53 views
2

我試圖讓我自定義的tableViewCell類中的單元格爲多行並換行。 我在這裏閱讀我的Q & A並按照說明進行操作,但仍無法找到解決方案。自定義TableViewCell中的標籤不能是多行或換行

我可以調整每個內容長度的動態高度,但仍無法使 這裏是我的代碼

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CustomItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomItemCell"]; 
    if(cell == nil){ 
     cell = [[CustomItemCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CustomItemCell"]; 

     //I set lineBreakMode of cell.postLabel to NSLineBreakByWordWrapping 
     // and set numberofLines = 0 ; 
     cell.postLabel.lineBreakMode = NSLineBreakByWordWrapping; 
     cell.postLabel.numberOfLines = 0; 
    } 
    CustomItem *item = [[channel items] objectAtIndex:[indexPath row]]; 
    //postLabel is the label that I want to be multiline and wordwrap 
    // postLabel get an NSString from [item post] 
    [[cell postLabel] setText:[item post]]; 

    [[cell timeLabel] setText:[item time]]; 
    [[cell distanceLabel] setText:[item distance]]; 
    [[cell thumbnailView] setImage:[item thumbnail]]; 

    return cell; 
} 

我創建了CustomItemCell.xib文件。實際上,我還將界限設置爲零,並在界面構建器中將換行符設置爲wordwrap,但未按預期顯示。

+0

標籤可以是多,如果你設置的標籤包裝根據其內容的高度 – 2013-03-26 11:05:52

+0

如果「postLabel」有問題,最簡單的方法是在設置文本後調用[[cell postLabel] sizeToFit]'。另外要注意在單元的contentView中添加'postLabel'。 – 2013-03-26 11:07:53

+0

高度已設置,但不會改變任何內容。 – 2013-03-27 02:25:24

回答

2

試試這個,

[cell.postLabel sizeToFit]; 
+0

已經嘗試過。但沒有工作......我把它放在下面[[cell postLabel] setText:[item post]];線。 – 2013-03-27 02:23:49

+0

我發現了這個問題。界面構建器啓用了「使用自動佈局」。我禁用了這個選項,sizeToFit完美地工作 – 2013-03-27 06:15:20

1

集單元格樣式爲多如下圖所示,

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; 

檢查..

+0

已經嘗試過。但沒有工作... – 2013-03-27 02:23:11

相關問題