简体   繁体   English

UIImageView在具有可变高度的自定义UITableViewCell中自动调整大小

[英]UIImageView auto resize inside a custom UITableViewCell with varying height

I have set up a custom cell prototype inside storyboard not using autolayout and i have run into a rather vexing problem where a UIImageView i have inside the cell seems to obey constraints all of it's own when i want it to reflect the size of the cell with a bit of padding. 我没有使用自动布局在情节提要板中设置了一个自定义单元格原型,并且遇到了一个非常棘手的问题,当我希望它反映单元格的大小时,单元格内的UIImageView似乎服从所有它自己的约束。一点填充。

Here is the setup in storyboard interface builder and view mode is set to "scale to fill" 这是故事板界面构建器中的设置,并且视图模式设置为“缩放以填充”

设定

And here is what i get back, with no editing of the placement or scaling of the image, it seems very much like it's using aspect fill. 这就是我返回的内容,无需编辑图像的位置或缩放比例,似乎非常像是在使用宽高比填充。

结果

I have tried various options and none seem to work for me so far, The only other way i can think is turn of auto resizing and set the rect myself manually when the cell is created any ideas? 我已经尝试了各种选项,但到目前为止,似乎没有一个对我有用。我能想到的唯一另一种方法是轮到自动调整大小并在创建单元格时手动设置自己的矩形?

and my code for setting the cell 和我设置单元格的代码

- (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];
}

and the protocol methods 和协议方法

-(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;
}

Set autoresizing properties like this, may you get help from this. 设置这样的自动调整大小属性,可能会从中获得帮助。

自动尺寸

I could not get AutoResizing to work and i tried another test project from scratch and it seemed to work, so it must have been certain settings, either way i ended up having to set the height manually for each cell based on it's content anyway here is the code to do so 我无法使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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有UIImageView的快速自定义UITableViewCell。 需要将单元格调整为图像高度 - Swift, custom UITableViewCell with UIImageView. Need to resize cell to image height 如何以不同的高度布局自定义UITableViewCell - How to layout custom UITableViewCell with varying height 自定义 UIImageView 不是自定义 UITableViewCell 内的圆圈 - Custom UIImageView is not circle inside custom UITableViewCell 无法在具有动态高度的子类UITableViewCell中调整UIImageView的框架大小 - Can't resize UIImageView's frame in subclassed UITableViewCell with dynamic height 如何在UITableViewCell中调整UIImageView的大小? - How to resize UIImageView in UITableViewCell? 无法更改UITableViewCell内部的UIImageView的宽度和高度-Swift - Unable to change the width and height of UIImageView inside a UITableViewCell - swift 如何使用自动布局调整固定宽度和高度的UIImageView的大小 - How to resize a fixed width and height UIImageView with auto layout 如何通过使用自动布局显示和隐藏组件来调整UITableViewCell高度的大小 - How to resize UITableViewCell height by showing and hiding components using auto layout 在UITableViewCell内部调整UIImageView的大小 - Resizing UIImageView inside a UITableViewCell 自定义UITableViewCell与零UIImageView - Custom UITableViewCell with nil UIImageView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM