设置在tableview中添加到单元格的图像的大小

我正在从plist中将图像添加到我的单元格视图中。 此外,我对使用url保存到plist的图像也有同样的问题。 它们的大小不同。 我想将它们调整到一定的大小,以便它们看起来都一样。 我试过用:

cell.imageView.contentMode = UIViewContentModeScaleAspectFill; cell.imageView.clipsToBounds = YES; 

它不起作用。 我尝试过其他一些东西并没有成功。 我查了一堆东西,找不到任何有用的东西。 这是我的行方法单元格:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier]; } if (tableView == self.searchDisplayController.searchResultsTableView) { cell.textLabel.text = [[searchResults objectAtIndex:indexPath.row] valueForKey:@"city"]; cell.detailTextLabel.text = [[searchResults objectAtIndex:indexPath.row] valueForKey:@"state"]; cell.imageView.image = [UIImage imageNamed:[[self.searchResults objectAtIndex:indexPath.row] valueForKey:@"cityImage"]]; cell.imageView.contentMode = UIViewContentModeScaleAspectFill; cell.imageView.clipsToBounds = YES; } else { cell.textLabel.text = [[content objectAtIndex:indexPath.row] valueForKey:@"city"]; cell.detailTextLabel.text = [[content objectAtIndex:indexPath.row] valueForKey:@"state"]; // Construct the expected URL for the image. NSURL *imageFileURL = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask][0]; imageFileURL = [imageFileURL URLByAppendingPathComponent:[[self.content objectAtIndex:indexPath.row] valueForKey:@"cityImage"] isDirectory:NO]; // Attempt to load the image at the above URL. cell.imageView.image = [[UIImage alloc] initWithContentsOfFile:[imageFileURL path]]; if (!cell.imageView.image) cell.imageView.image = [UIImage imageNamed:[[self.content objectAtIndex:indexPath.row] valueForKey:@"cityImage"]]; cell.imageView.contentMode = UIViewContentModeScaleAspectFill; cell.imageView.clipsToBounds = YES; } return cell; } 

对于我的生活,我无法弄清楚这一点。 我知道这可能是一个简单的修复,但我想不出别的。

编辑:

正如所建议的那样,我创建了一个自定义单元格,在那里声明了layoutSubviews,然后按如下方式对单元格进行了子类化:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; MyCostumCell *cell = (MyCostumCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[MyCostumCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier]; } if (tableView == self.searchDisplayController.searchResultsTableView) { cell.textLabel.text = [[searchResults objectAtIndex:indexPath.row] valueForKey:@"city"]; cell.detailTextLabel.text = [[searchResults objectAtIndex:indexPath.row] valueForKey:@"state"]; cell.imageView.image = [UIImage imageNamed:[[self.searchResults objectAtIndex:indexPath.row] valueForKey:@"cityImage"]]; //cell.textLabel.textColor = [UIColor colorWithRed:153.0/255.0f green:0.0/255.0f blue:0.0/255.0f alpha:1]; } else { cell.textLabel.text = [[content objectAtIndex:indexPath.row] valueForKey:@"city"]; cell.detailTextLabel.text = [[content objectAtIndex:indexPath.row] valueForKey:@"state"]; // Construct the expected URL for the image. NSURL *imageFileURL = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask][0]; imageFileURL = [imageFileURL URLByAppendingPathComponent:[[self.content objectAtIndex:indexPath.row] valueForKey:@"cityImage"] isDirectory:NO]; // Attempt to load the image at the above URL. cell.imageView.image = [[UIImage alloc] initWithContentsOfFile:[imageFileURL path]]; if (!cell.imageView.image) cell.imageView.image = [UIImage imageNamed:[[self.content objectAtIndex:indexPath.row] valueForKey:@"cityImage"]]; } return cell; } 

再一次,它没有用。 这是屏幕截图:

在此处输入图像描述

你会inheritanceUITableViewCell。 但你不必创建Dante提出的全新视图,而是覆盖-layoutSubviews

 @interface VideoItemCell : UITableViewCell @end @implementation VideoItemCell -(void)layoutSubviews { [super layoutSubviews]; self.imageView.contentMode = UIViewContentModeScaleAspectFill; self.imageView.frame = CGRectMake(self.imageView.frame.origin.x, self.imageView.frame.origin.y, 100, 100); } @end 

在tableview控制器中

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; VideoItemCell *cell = (VideoItemCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[VideoItemCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier]; } //… return cell; } 

默认TableViewCell根据图像大小调整imageview的大小。 要解决此问题,您可以使用具有自定义图像视图的自定义单元格。

有关如何创建自定义单元格的更多信息

如何从Xib文件加载自定义UITableViewCells?