2013-04-26 109 views
4

我的自定義UITableView出現問題。我想知道如何在沒有看到任何橢圓「...」的情況下正確地將一組文本放入單元格中,並且沒有在單元格末尾切斷文本。UITableViewCell中的文本換行不正確

這是我的手機是什麼樣子,目前:

enter image description here

這是一個UISplitViewController的一部分。問題在於,之前由於某種原因,它會顯示文本的整個長度,但它會到達單元格的末尾,並且字符串的其餘部分被切斷(這發生在我檢查「AutoLayout」時)。

這是我的代碼看起來像現在:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"BCell"; 

    BracketTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if(cell == nil) 
    { 
     cell = [[BracketTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     [cell.description setLineBreakMode:NSLineBreakByWordWrapping]; 
     cell.description.numberOfLines = 0; 
     cell.description.font = [UIFont fontWithName:@"Helvetica" size:14.0]; 
    } 

    Bracket *bracket = [brackets objectAtIndex:indexPath.row]; 

    [cell.description setText:bracket.name]; 
    [cell.bracketId setText:[NSString stringWithFormat:@"%@", bracket.bracketId]];  

    return cell; 
} 

我嘗試對身高,但似乎並不重要,因爲我可以設置高度什麼的,但它仍然顯示的文本被截斷。

謝謝!

+0

你打算使用AUTOL的ayout?我知道你說過你開啓了它並引發了問題,但我不確定這是否是一個調試步驟,或者是否始終需要它。你可以發佈'BracketTableCell'的代碼嗎? – jszumski 2013-04-29 18:02:08

+0

我在BracketTableCell上沒有任何代碼。這是一個定製的表格單元類,具有描述和圖標的屬性。而已。沒有添加其他代碼。除非我應該?哦,如果關閉自動佈局可以給我更多的控制權,那麼我會這樣做。 – gdubs 2013-04-29 18:38:19

+1

你可以發佈嗎?什麼是「描述」......一個「UILabel」?通常情況下,你永遠不想命名一個屬性,因爲它是'NSObject'上的一個方法。我試圖弄清楚你如何確定這個標籤。 – jszumski 2013-05-02 20:21:13

回答

0

@gdubs可以使用自定義UITableViewCells

參考,您可以使用Customize Table View Cells for UITableView

我想這將是您輕鬆定製UILabels然後。就像如果你想添加多條線然後設置TitletLabel.numberOfLines=0;,並且如果你想要包裝TitleLabel.lineBreakMode=NSLineBreakByWordWrapping;。文字換行中還有其他選項。

+0

我就是這樣做的。但它仍然會顯示相同的方式。它也不會調整高度。如果你注意到它在這裏>> cell.description.numberOfLines = 0; cell.description.font = [UIFont fontWithName:@「Helvetica」size:14.0]; – gdubs 2013-04-29 18:37:02

+0

@gdubs我不按照我給出的參考鏈接嘗試自定義單元格。它有適當的解釋和更清潔的實施。 – Akshay 2013-04-29 18:50:30

+0

這是一回事。這就是我學習在桌子上應用自定義單元格的地方。 – gdubs 2013-04-29 19:24:36

0

帶標籤和Autolayout的快樂的關鍵是在標籤上設置preferredMaxLayoutWidth屬性。如果沒有這個標籤,不要包裝得當(或者根本沒有包裝,在某些情況下,這是你之前看到的,我想呢?)。

將值設置爲最大線寬,然後標籤應該正常工作。

0

我認爲這個問題必須做你的標籤寬度,如果您正在使用自動佈局擴展標籤的寬度,以填補母細胞,並添加拖尾和先行到上海華約束,使之重新調整用它。

1

通常我的方法支持可變的高度細胞是定義一個類方法,可以計算上漿對於給定的模型對象:

+ (CGFloat)heightForBracket:(Bracket*)bracket; 

使其成爲一個類方法的優點是可以共享常數(填充值,字體大小,縮進級別等)與您的代碼實際上實現佈局,而不必暴露給任何其他類。如果您想在將來更改這些常量,則只需在單元格子類中的某個位置進行更改即可。一個例子子類實現:

#define kPaddingHorizontal 10.0 
#define kPaddingVertical 10.0 
#define kFontSizeName 17.0 

+ (CGFloat)heightForBracket:(Bracket*)bracket { 
    // determine the dimensions of the name 
    UIFont *nameFont = [UIFont systemFontOfSize:kFontSizeName]; 
    CGFloat nameSize = [bracket.name sizeWithFont:nameFont 
           constrainedToSize:CGSizeMake(300, CGFLOAT_MAX) // 300 is the width of your eventual label 
            lineBreakMode:NSLineBreakByWordWrapping]; 

    // Apple recommends all cells be at least 44px tall, so we enforce a minimum here 
    return MAX(44, nameSize.height + 20 + kPaddingVertical*2); // 20 is space for the subtitle label 
} 

- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier { 
    self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier]; 

    if (self) { 
     // bracket name 
     self.textLabel.numberOfLines = 0; // 0 makes this variable height 
     self.textLabel.font = [UIFont systemFontOfSize:kFontSizeName]; 
     self.textLabel.lineBreakMode = NSLineBreakByTruncatingTail; 
     self.textLabel.backgroundColor = [UIColor clearColor]; 
     // if you wanted to hardcode a specific width, to a subview do it here as a constant and then share it with heightForBracket: 

     // bracket number 
     self.detailTextLabel.numberOfLines = 1; 
     self.detailTextLabel.font = [UIFont systemFontOfSize:14.0]; 
     self.detailTextLabel.lineBreakMode = NSLineBreakByTruncatingTail; 
     self.detailTextLabel.backgroundColor = [UIColor clearColor]; 
    } 

    return self; 
} 

- (void)setBracket:(Bracket*)bracket { 
    _bracket = bracket; 

    self.textLabel.text = bracket.name; 
    self.detailTextLabel.text = [NSString stringWithFormat:@"%@", bracket.bracketId]; 
} 

然後,您可以撥打heightForBracket:tableView:heightForRowAtIndexPath:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    Bracket *bracket = [brackets objectAtIndex:indexPath.row]; 

    return [BracketTableCell heightForBracket:bracket]; 
} 

tableView:cellForRowAtIndexPath:變得非常容易,只需設置合適的支架上的細胞:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"BCell"; 
    BracketTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[BracketTableCell alloc] initWithReuseIdentifier:CellIdentifier]; 
    } 

    Bracket *bracket = [brackets objectAtIndex:indexPath.row]; 
    cell.bracket = bracket; 

    return cell; 
} 

幾點說明:

  • 這個假設小區不使用自動佈局
  • 這明確地硬編碼的細胞/標籤的寬度,這可能會或可能不適合你的使用情況
  • ,因爲那是你不應該名稱的屬性description一個method that already exists on the NSObject protocol
  • 其他增強功能將被緩存的heightForBracket:結果,以提高滾動性能,尤其是當你開始做上漿邏輯一噸的子視圖
+0

我會盡快嘗試。我會盡快回復你! – gdubs 2013-05-07 13:08:16