gpt4 book ai didi

ios - 设置添加到表格 View 中单元格的图像的大小

转载 作者:行者123 更新时间:2023-12-01 17:26:02 25 4
gpt4 key购买 nike

我正在从 plist 将图像添加到我的单元格 View 中。我也使用 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;
}

再次,它没有工作。这是屏幕截图:

enter image description here

最佳答案

你会继承 UITableViewCell。但是您不必按照 Dante 的建议创建一个全新的 View 层次结构,而是覆盖 -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 Controller 中
- (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;
}

关于ios - 设置添加到表格 View 中单元格的图像的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19253239/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com