UITableViewCell:单元在重新加载数据调用时收缩

时间:2015-07-28 11:34:41

标签: ios objective-c uitableview layoutsubviews

我正在尝试制作聊天应用程序。我填充了来自笔尖的聊天气泡。

但每当我向数组添加一条新消息并调用reloadData方法时,它上面的单元格就会缩小。

这是我的UITableView cellForRowAtIndexPath的代码:

CGRect expectedLabelSize = [messageBody boundingRectWithSize:CGSizeMake(200, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:13]} context:nil];

if(expectedLabelSize.size.height > 15)
    return expectedLabelSize.size.height + 55;
else
    return 65;

这是heightForRowAtIndexPath的代码

- (void)awakeFromNib {
    // Initialization code
}

- (void) layoutSubviews{
    [super layoutSubviews];

    CGRect expectedLabelSize = [self.message boundingRectWithSize:CGSizeMake(200, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:13]} context:nil];

    if(expectedLabelSize.size.height > 15)
    {
        [LabelMessage setFrame:CGRectMake(50, 30, 200, expectedLabelSize.size.height + 15)];
        [ImageViewChatBox setFrame:CGRectMake(40, 10, 220, expectedLabelSize.size.height + 45)];
    }

    [LabelMessage setText:self.message];
    [LabelDateTimeStamp setText:self.dateTimeStamp];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}

- (void) initWithMessage:(NSString *)message andDateTimeStamp:(NSString *)dateTimeStamp{
    self.message = message;
    self.dateTimeStamp = dateTimeStamp;
}

此处还有SentMessageTableViewCell xib实现的代码:

The default in the wiredtiger_open call for transaction_sync 
is enabled=false, method=fsync. When the setting is enabled=false the 
log writes should not be synchronous until someone uses a transaction 
with sync=true. 

More info: https://jira.mongodb.org/browse/WT-1949

Info on Mongdb logging:
http://blog.mongodb.org/post/172254834/mongodb-is-fantastic-for-logging
http://docs.mongodb.org/ecosystem/use-cases/storing-log-data/
http://docs.mongodb.org/manual/tutorial/rotate-log-files/

但是当我运行应用程序时,结果是这样的。

enter image description here

上面的单元格缩小为默认大小。

编辑:这是我的xib。

enter image description here

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

您需要更改layoutSubviews方法

- (void) layoutSubviews{
    [super layoutSubviews];

    CGRect expectedLabelSize = [self.message boundingRectWithSize:CGSizeMake(200, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:13]} context:nil];

    if(expectedLabelSize.size.height > 15)
    {
        [LabelMessage setFrame:CGRectMake(50, 30, 200, expectedLabelSize.size.height + 15)];
        [ImageViewChatBox setFrame:CGRectMake(40, 10, 220, expectedLabelSize.size.height + 45)];
    } else {

          // Need to set frame for normal height messages
    }

    [LabelMessage setText:self.message];
    [LabelDateTimeStamp setText:self.dateTimeStamp];
}

答案 1 :(得分:1)

原始评论:您可以为内部元素添加一些约束并尝试吗?

答案: xib文件缺少约束。 iOS需要正确的约束来适应帧后的内部元素(在这种情况下高度变化)会发生变化。