[英]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个问题:
contentMode
of the image view to UIViewContentModeScaleToFill
(this is the default, but it's better define this kind of properties anyway). contentMode
设置为UIViewContentModeScaleToFill
(这是默认设置,但无论如何最好定义这种属性)。 _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.