1

所以在一个UITableViewCell子类中这很好用:

- (void)awakeFromNib {
    [super awakeFromNib];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"table-highlight.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)]];
    imageView.contentMode = UIViewContentModeScaleToFill;
    self.selectedBackgroundView = imageView;
}

但是,我有一个 tableView,它只使用没有子类的标准表格单元格。我已经尝试了在其他答案中找到的各种组合,但没有运气:

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"table-highlight.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)]];
    imageView.contentMode = UIViewContentModeScaleToFill;
    cell.selectedBackgroundView = imageView;
}
4

1 回答 1

1

该死。发布此消息 30 秒后想到一件事。留下它以防它帮助别人。我的 imageView 上没有框架。工作代码:

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"table-highlight.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)]];
    imageView.frame = cell.frame;
    imageView.contentMode = UIViewContentModeScaleToFill;
    cell.selectedBackgroundView = imageView;
}
于 2012-05-02T23:07:29.430 回答