【发布时间】:2015-05-31 09:52:39
【问题描述】:
我用一个单独的 UILabel 类定义单元格中我的标签的边距:
- (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;
}
【问题讨论】:
标签: ios objective-c uitableview uilabel