2015-05-31 58 views
0

我與單獨UILabel類定義裕度從我的標籤在Cell:UITableView的標籤字包裹

- (void)drawTextInRect:(CGRect)rect { 
    UIEdgeInsets insets = {5, 10,5, 10}; 
    [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)]; 
} 

隨着下面的代碼,我從小區定義高度:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    if (!self.customCell) { 
     self.customCell = [self.chatTableView dequeueReusableCellWithIdentifier:@"CellChat"]; 
    } 

    self.customCell.sender.text = [array objectAtIndex:indexPath.row]; 

    [self.customCell layoutIfNeeded]; 

    CGFloat height = [self.customCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height; 

    return height +1; 
} 

我問題:當我測試應用程序時,它不會在正確的地方使用單詞換行。爲什麼?它與UILabel類有什麼關係?

編輯:

謝謝,但我仍然無法正常工作。我沒有錯誤,但當我加載表格視圖時,它不顯示文字。此刻,我的代碼看起來像這樣:

進口:

#import "ChatViewController.h" 
#import "ChatTableViewCell.h" 

@interface ChatViewController() 

@property (strong, nonatomic) ChatTableViewCell *customCell; 

@end 

下一頁:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    UILabel *gettingSizeLabel = [[UILabel alloc] init]; 
    gettingSizeLabel.font = [UIFont fontWithName:@"Arial" size:15]; 
    PFObject *tempObject = [array objectAtIndex:indexPath.row]; 
    gettingSizeLabel.text = [tempObject objectForKey:@"Sender"]; 
    gettingSizeLabel.numberOfLines = 0; 

    CGSize maximumLabelSize = CGSizeMake(140, MAXFLOAT); 
    CGSize expectedSize = [gettingSizeLabel sizeThatFits:maximumLabelSize]; 

    return expectedSize.height + (10*2); 
} 

我的cellForRowAtIndexPath:

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

    ChatTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifer]; 

    PFObject *tempObject = [array objectAtIndex:indexPath.row]; 
    cell.sender.text = [tempObject objectForKey:@"Sender"]; 
    cell.receptor.text = [tempObject objectForKey:@"Receptor"]; 
    cell.sender.lineBreakMode = NSLineBreakByWordWrapping; 
    cell.sender.numberOfLines = 0; 

    return cell; 
} 

回答

1

編輯

不要使用self.customCell- (CGFloat)tableView:tableView heightForRowAtIndexPath:indexPath

相反,計算單元格高度是這樣的:

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

    UILabel *gettingSizeLabel = [[UILabel alloc] init]; 
    gettingSizeLabel.font = font; 
    gettingSizeLabel.text = [array objectAtIndex:indexPath.row]; // your text inside [array objectAtIndex:indexPath.row]; 
    gettingSizeLabel.numberOfLines = 0; 

    CGSize maximumLabelSize = CGSizeMake(yourMaxWidth, MAXFLOAT); 

    CGSize expectedSize = [gettingSizeLabel sizeThatFits:maximumLabelSize]; 

    return expectedSize.height /*then add your margin 'y' margin multiplied by two for the top and bottom margin*/; 
} 

不要忘記設置

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

    self.customCell = [tableView dequeueReusableCellWithIdentifier:cellID]; 

    if (!self.customCell) 
    { 
     self.customCell = [[YourCellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID]; 
    } 

    // dont forget to set this 

    self.customCell.textLabel.lineBreakMode = NSLineBreakByWordWrapping; 
    self.customCell.textLabel.numberOfLines = 0; 
    // [self.customCell sizeToFit]; optional depending from what you want 
    return self.customCell; 
} 

希望這有助於你,快樂編碼..乾杯! :)

+2

是正確的。當調用heightForRowAtIndexPath時,您的單元格尚未初始化。它的第一個heightForRowAtIndexPath將被調用,然後cellForRowAtIndexPath。 –