在 UITableView 中正确设置 UIImageVIew 的高度和宽度的方法是什么?

What is the way to properly set the height and width of a UIImageVIew in a UITableView?

我在尝试使 TableView 中的图像不超大时遇到问题。我将 UIImageView 拖放到 UITableViewCell 中,并直观地设置高度和宽度(我正在使用故事板)。但是当我 运行 应用程序时,图像看起来太大了。无论我做什么(设置约束、CGRectMake 等方法),图像看起来总是一样的。

这是我的 tableView cellForRowAtIndexPath 方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = @"cell";


TFGResultsTableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];

if(cell == nil) {
    cell = [[TFGResultsTableViewCell alloc]
            initWithStyle:UITableViewCellStyleDefault
            reuseIdentifier:cellID];
}
// Configure the cell...
cell.eventnamelabel.text = [eventnameArray objectAtIndex: [indexPath row]];
cell.userLabel.text = [userArray objectAtIndex: [indexPath row]];
cell.sportnameLabel.text = [sportnameArray objectAtIndex: [indexPath row]];

if([cell.sportnameLabel.text isEqualToString:@"Soccer"]){

    // Here I should do something to resize the image!
    cell.imageView.image = [UIImage imageNamed:@"icon.png"];
}

return cell;
}

这些是我当前图像设置的屏幕截图:

终于找到解决办法了:

cell.imageView.image = [UIImage imageNamed:@"icon.png"]; 

应该重写为:

cell.eventImage.image = [UIImage imageNamed:@"icon.png"];

在您的 UITableViewCell 中,您为 eventImage 创建了一个 IBOutlet。但是在cellForRowAtIndexPath你这样做,

cell.imageView.image = [UIImage imageNamed:@"icon.png"];

以上方式错误。你应该按照下面的方式做。

cell.eventImage.image = [UIImage imageNamed:@"icon.png"];