我用一个单独的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;
}发布于 2015-05-31 10:09:14
编辑
不要在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;
}希望这对你有帮助,快乐的编码..干杯!:)
https://stackoverflow.com/questions/30556074
复制相似问题