[英]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.