UIImageView在不同高度的自定义UITableViewCell内自动调整大小

时间:2015-02-02 04:09:02

标签: ios objective-c uitableview uiimageview custom-cell

我在故事板里面设置了一个不使用自动布局的自定义单元格原型,我遇到了一个相当棘手的问题,我在单元格内部的UIImageView似乎服从了我想要它的所有约束用一点填充来反映单元格的大小。

以下是故事板界面构建器中的设置,并且视图模式设置为"缩放以填充"

Setup

以下是我回来的情况,没有编辑图像的位置或缩放,看起来非常像使用纵横填充。

Result

我已经尝试了各种选项,到目前为止似乎没有一个对我有用,我能想到的另一种方法是自动调整大小并在创建单元格时手动设置矩形吗?

和我设置单元格的代码

- (void)setCell:(NSDictionary*)messageData
{
    [_nameLabel setText:messageData[@"name"]];
    [_messageLabel setText:messageData[@"message"]];

    [_nameLabel sizeToFit];
    [_messageLabel sizeToFit];

    UIImage *image;
    if([messageData[@"direction"] boolValue])
    {
        image= [[UIImage imageNamed:@"bubble_left"]resizableImageWithCapInsets:UIEdgeInsetsMake(18, 6, 6, 10) resizingMode:UIImageResizingModeStretch];
    }
    else
    {
        image= [[UIImage imageNamed:@"bubble_right"]resizableImageWithCapInsets:UIEdgeInsetsMake(18, 10, 6, 6) resizingMode:UIImageResizingModeStretch];
    }

    [_bubbleImage setImage:image];
}

和协议方法

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CUIChatCellTableViewCell *cell = [_chatTableView dequeueReusableCellWithIdentifier:@"CustomCell" forIndexPath:indexPath];

    NSDictionary *messageData = _testDataArray[indexPath.row];

    [cell setCell:messageData];

    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    _customCell = [_chatTableView dequeueReusableCellWithIdentifier:@"CustomCell"];

    //configure cell
    NSDictionary *messageData = _testDataArray[indexPath.row];

    [_customCell setCell:messageData];

    //Get height of the cell
    CGFloat height = [_customCell.messageLabel frame].size.height+[_customCell.nameLabel frame].size.height+20;

    return height;
}

2 个答案:

答案 0 :(得分:1)

设置这样的自动调整属性,可以从中获得帮助。

Autoresizing

答案 1 :(得分:0)

我无法使AutoResizing工作,我尝试从头开始另一个测试项目,它似乎工作,所以它一定是某些设置,无论哪种方式我最终必须根据它的内容手动设置每个单元格的高度无论如何,这里是代码

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    _customCell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell"];

    //configure cell
    CMessageInfo *messageData = _testDataArray[indexPath.row];

    [_customCell setCell:messageData];

    //Get height of the cell
    CGFloat height = [_customCell.messageLabel frame].size.height+[_customCell.nameLabel frame].size.height+20;

    return height;
}