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;
}
是正確的。當調用heightForRowAtIndexPath時,您的單元格尚未初始化。它的第一個heightForRowAtIndexPath將被調用,然後cellForRowAtIndexPath。 –