我正在从plist中将图像添加到我的单元格视图中。此外,我对使用网址保存到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;
}
同样,它没有用。这是屏幕截图:
答案 0 :(得分:7)
你会继承UITableViewCell。但你不必创建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;
}
答案 1 :(得分:1)
默认UITableViewCell根据图像大小调整图像视图大小。要解决此问题,您可以使用具有自定义图像视图的自定义单元格。
有关如何创建自定义单元格的更多信息