简体   繁体   English

无法在具有动态高度的子类UITableViewCell中调整UIImageView的框架大小

[英]Can't resize UIImageView's frame in subclassed UITableViewCell with dynamic height

In my UITableView , all the cells will be a different height. 在我的UITableView ,所有单元格将具有不同的高度。 All of them have a background image, which is a bubble picture. 它们都具有背景图像,即气泡图片。 All of the cell's TextLabels need to be different widths as well, so I've subclassed UITableViewCell and have given it two properties: 单元的所有TextLabels需要具有不同的宽度,因此我将UITableViewCell子类化,并为其提供了两个属性:

@property (strong, nonatomic) UILabel* messageTextLabel;
@property (strong, nonatomic) UIImageView* bubbleImageView;

I have my custom UITableViewCell 's initWithStyle set up like so: 我将自定义UITableViewCell的initWithStyle设置如下:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {

    //stretchable bubble image
    UIImage *rawBackground = [UIImage imageNamed:@"text_bubble"];
    UIImage *background = [rawBackground stretchableImageWithLeftCapWidth:13 topCapHeight:22];
    _bubbleImageView = [[UIImageView alloc] initWithImage:background];

     //textlabel
    _messageTextLabel = [[UILabel alloc] init];
    [_messageTextLabel setFont:[UIFont systemFontOfSize:14.0f]];
    [_messageTextLabel setNumberOfLines:0];
    [_messageTextLabel setBackgroundColor:[UIColor clearColor]];
    [_messageTextLabel setTextColor:[UIColor blackColor]];

    [self.contentView addSubview:_bubbleImageView];
    [self.contentView addSubview:_messageTextLabel];

}
    return self;
}

In my TableView's cellForRowAtIndexPath , I've already tried to just alloc a UIImageView there and set it to the cell's frame, and that works, but when I scroll up and down then the screen gets cluttered with UIImageViews being re-alloced over and over. 在我的TableView的cellForRowAtIndexPath ,我已经尝试过在其中分配一个UIImageView并将其设置为单元格的框架,并且可以正常工作,但是当我上下滚动时,屏幕变得混乱,一遍又一遍地重新分配UIImageViews I've tried the if(cell == nil) technique, but that just makes my entire UITableView blank. 我已经尝试过if(cell == nil)技术,但这只是使我的整个UITableView空白。 So, this is what I have right now: 所以,这就是我现在所拥有的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    Cell *cell = (Cell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    //works just fine
    [cell.messageTextLabel setFrame:CGRectMake(15, 0, 245, cell.frame.size.height -10)];

    //this doesn't work at all
    [cell.bubbleImageView setFrame:CGRectMake(10, 0, cell.frame.size.width, cell.frame.size.height)];

    [cell.messageTextLabel setText:[self.randomData objectAtIndex:indexPath.item]];

    return cell;
}

So my UITableView ends up looking like this when run (cell selected so you can see the frame): 所以我的UITableView最终在运行时看起来像这样(选中了单元格,因此您可以看到框架):

Note that I can set the frame of the bubbleImageView in my UITableViewCell subclass, but I don't know what the cell's height is going to be then. 请注意,我可以在UITableViewCell子类中设置bubbleImageView的框架,但是我不知道单元格的高度将是多少。 I'm more baffled by the fact that I can set my messageTextLabel 's frame and not my bubbleImageViews. 我对我可以设置messageTextLabel的框架而不是我的bubbleImageViews感到困惑。 I may be doing something that is very elementary wrong, it's a simple problem though for some reason I'm getting tripped up. 我可能正在做一些非常基本的错误,这是一个简单的问题,尽管由于某种原因我被绊倒了。

Thank you! 谢谢!

I have seen 2 problems here: 我在这里看到2个问题:

  • I think that you have the auto-layout enabled. 我认为您已启用自动版式。
    In this case the initial frame and the autoresizingMask are translated to auto-layout constraints automatically. 在这种情况下,初始帧和autoresizingMask会自动转换为自动布局约束。
    After this no frame changes will change the view's size and location - only the constraints... 此后,没有框架更改将更改视图的大小和位置-仅约束...
  • You haven't set the contentMode of the image view to UIViewContentModeScaleToFill (this is the default, but it's better define this kind of properties anyway). 您尚未将图像视图的contentMode设置为UIViewContentModeScaleToFill (这是默认设置,但无论如何最好定义这种属性)。
    Just add this line: _bubbleImageView.contentMode = UIViewContentModeScaleToFill; 只需添加以下行: _bubbleImageView.contentMode = UIViewContentModeScaleToFill; right after its initiation. 在启动之后

I think that the solution here is auto-layout - setting constraints properly will solve this issue. 我认为这里的解决方案是自动布局-正确设置约束将解决此问题。
I suggest doing it in the storyboard, with table view controller and dynamic cells... 我建议在情节提要中使用表格视图控制器和动态单元格来执行此操作...

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

相关问题 带有UIImageView的快速自定义UITableViewCell。 需要将单元格调整为图像高度 - Swift, custom UITableViewCell with UIImageView. Need to resize cell to image height 调整子类化的UITableViewCell UILabel框架 - Adjusting Subclassed UITableViewCell UILabel Frame UIImageView在具有可变高度的自定义UITableViewCell中自动调整大小 - UIImageView auto resize inside a custom UITableViewCell with varying height UIImageView框架不是动态的 - UIImageView frame isn't dynamic 约束UIImageView高度以尊重UITableViewCell中的动态UILabel高度 - Constraining UIImageView height to respect dynamic UILabel height in UITableViewCell 如何在UITableViewCell中调整UIImageView的大小? - How to resize UIImageView in UITableViewCell? 如何根据动态图像URL的高度来调整包含在UITableview的自定义单元格中的UIImageview的大小? - How to resize an UIImageview, enclosed within a UITableview's custom cell, depending upon dynamic image URL's height? UITableViewCell子类化,当选择时消失UIImageView - UITableViewCell subclassed, when selected disappears UIImageView UIImageView不会调整大小以匹配UIView的框架 - UIImageView will not resize to match UIView's frame 我需要设置UITableViewCell动态高度,并使用UIImageView设置多个动态标签 - I need to set UITableViewCell Dynamic Height, With Multiple dynamic label with UIImageView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM