UIView未通过其子视图UILabel正确调整大小

时间:2015-04-02 06:55:33

标签: ios uitableview interface-builder autolayout uilabel

我正在聊天,这次我决定使用Autolayout,你可能会注意到我对这个主题很新。我的问题是我无法在表视图中获得具有标签作为子视图的视图。有时候它比较大,你可以从下面的图片中看到,有时它更小,这使得标签缩小到一行的一个字符。

enter image description here

enter image description here

我正在使用界面构建器来制定约束。您可以在下面的屏幕截图中查看它们。

enter image description here enter image description here

这是我用于表视图委托方法的代码:

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

    BubbleCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    Message *message = [_messageList objectAtIndex:indexPath.row];

    if ([message ownMessage]) {
        cell.leftView.hidden = YES;
        cell.rightView.hidden = NO;
        cell.leftTextLbl.hidden = YES;
        cell.rightTextLbl.hidden = NO;
        cell.rightTextLbl.text = [message messageString];
    }
    else {
        cell.leftView.hidden = NO;
        cell.rightView.hidden = YES;
        cell.leftTextLbl.hidden = NO;
        cell.rightTextLbl.hidden = YES;
        cell.leftTextLbl.text = [message messageString];
    }
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_messageList count];
}

我的自定义单元格实现为空,我只是在合成属性。我在设置标签文本后试图调用layoutIfNeeded,但这根本没有帮助。可能是这个问题的根源是什么?如果我使用预加载的消息,则每条消息都会正确显示,但是当我开始编写和添加新消息时,问题就会开始。我在添加新消息时做错了什么?

Message *message = [[Message alloc]initWithMessageString:text ownMessage:YES];
        [_messageList addObject:message];
        [_tableView reloadData];
        [_tableView layoutIfNeeded];
        NSInteger lastSectionIndex = [_tableView numberOfSections] - 1;
        // Then grab the number of rows in the last section
        NSInteger lastRowIndex = [_tableView numberOfRowsInSection:lastSectionIndex] - 1;
        NSIndexPath *lastIndexPath = [NSIndexPath indexPathForRow:lastRowIndex inSection:lastSectionIndex];
        [_tableView scrollToRowAtIndexPath:lastIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];

0 个答案:

没有答案
相关问题