UITableView单元格UIImageView在选中时会随机更改其框架

时间:2021-02-06 21:07:38

I'm struggling with the following problem: I'm downloading a title and an imageUrl from Json and I'm setting them inside my UITableView custom cell with the following method:

我正在努力解决以下问题:我正在从Json下载标题和imageUrl,并使用以下方法将它们设置在我的UITableView自定义单元格中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Retrieve cell
    NSString *cellIdentifier = @"cell";
    UITableViewCell *categoryCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    categoryCell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
    categoryCell.textLabel.numberOfLines = 0;

    // Get the location to be shown
    RecipesList *item = _feedRecipesList[indexPath.row];




    // Get references to labels of cell
    categoryCell.textLabel.text = item.name;
    item.urlImg = [item.urlImg stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
    [categoryCell.imageView sd_setImageWithURL:[NSURL URLWithString:item.urlImg]
                placeholderImage:[UIImage imageNamed:@"Placeholder.png"]];



    return categoryCell;
}

Everything works fine but when I select a cell (so it gets highlighted) the UIImageView frame of the cell changes it's size for no reasons. At first I was using basic cell and I thought the problem was caused by that so I tried with custom cell and on the custom class I added on the .m file the following code:

一切正常但是当我选择一个单元格(因此它被突出显示)时,单元格的UIImageView框架会无缘无故地改变它的大小。起初我使用的是基本单元格,我认为这个问题是由于我尝试使用自定义单元格而在自定义类上我在.m文件中添加了以下代码:

- (void)layoutSubviews {
[super layoutSubviews];

self.imageView.frame = CGRectMake(0,0,55,55);

//Any additional setting that you want to do with image view

[self.imageView setAutoresizingMask:UIViewAutoresizingNone];

}

It does resize the UIImageView but again if i select a cell the image frame changes.

它会调整UIImageView的大小,但如果我选择一个单元格,图像框会发生变化。

Does anyone had the same issue?

有没有人有同样的问题?

And also in order to create the lazy loading i'm using the library SDWebImage but I don't think that the problem is related to that.

而且为了创建延迟加载我正在使用库SDWebImage,但我不认为问题与此有关。

2 个解决方案

#1


Strange behavior. Just a few suggestions, try this

奇怪的行为。只是一些建议,试试这个

self.autoresizesSubviews = NO;
self.contentView.clipsToBounds = YES;
self.imageView.contentMode = UIViewContentModeScaleAspectFit;

#2


God I finally resolved this, Instead of using the standard cell elements I've added a UIImageView and a UILabel to the cell in order to create a custom cell, then in order to show the image correctly I've added on the LayoutSubviews method cellImg.contentMode = UIViewContentModeScaleAspectFill;

上帝我终于解决了这个问题,而不是使用标准的单元格元素我已经在单元格中添加了UIImageView和UILabel以创建自定义单元格,然后为了正确显示图像我在LayoutSubviews方法上添加了单元格.contentMode = UIViewContentModeScaleAspectFill;

#1


Strange behavior. Just a few suggestions, try this

奇怪的行为。只是一些建议,试试这个

self.autoresizesSubviews = NO;
self.contentView.clipsToBounds = YES;
self.imageView.contentMode = UIViewContentModeScaleAspectFit;

#2


God I finally resolved this, Instead of using the standard cell elements I've added a UIImageView and a UILabel to the cell in order to create a custom cell, then in order to show the image correctly I've added on the LayoutSubviews method cellImg.contentMode = UIViewContentModeScaleAspectFill;

上帝我终于解决了这个问题,而不是使用标准的单元格元素我已经在单元格中添加了UIImageView和UILabel以创建自定义单元格,然后为了正确显示图像我在LayoutSubviews方法上添加了单元格.contentMode = UIViewContentModeScaleAspectFill;