【问题标题】:setting the size of the image added to cell in tableview设置添加到表格视图中单元格的图像的大小
【发布时间】:2013-10-08 16:27:55
【问题描述】:

我正在从 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;
}

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

【问题讨论】:

  • 您绝对应该阅读该纪录片。你的措辞完全混乱。在cellForRowAtIndexPath: 中,您不会对单元格进行子类化。 u 使用子类单元格。

标签: ios objective-c uitableview ios7


【解决方案1】:

你可以继承 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;
}

【讨论】:

  • 我尝试添加该方法,但我收到错误 no visible @interface for uitableview declares selector layoutsubview.
  • 你需要子类化单元格,而不是表格视图
  • 哥们,你不喜欢看说明书吧?对细胞进行子分类是非常标准的。但正如你非常有礼貌地问:给你。
  • 感谢您的出色表现并尽力提供帮助。但不知何故,它对我不起作用。我用屏幕截图发布了我的问题的编辑部分。再次感谢您的宝贵时间。
  • 如果您向我们展示您的 layoutSubviews 将会很有帮助(请不要告诉我,您没有根据您的需要调整它)
【解决方案2】:

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

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

How do you load custom UITableViewCells from Xib files?

【讨论】:

  • 感谢您的链接,但它太旧了,无法正常工作。我也尝试过通过图像视图创建图像并为其分配标签的自定义单元格。那也不起作用。你还有什么可以推荐的吗?
  • 答案仍然有效..您可以尝试通过谷歌逐步搜索适当的教程,这可以帮助您正确完成
猜你喜欢
  • 2011-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多