2

我的 UITableViewCells 都是预定义的“字幕”样式。我想将选定单元格的背景图像设置为另一张图片。我尝试了各种方法来实现这一点,并且在 stackoverflow 上讨论的所有方法似乎都失败了。我再次尝试了一些其他更简单的方法来更改 selectedBackgroundView 属性,例如:

cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.bounds] ;
cell.selectedBackgroundView.backgroundColor = [UIColor yellowColor];

但它也不起作用。那有什么问题?

4

2 回答 2

18

据我了解,您想将选定的背景图像设置为您的单元格?

cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"backgroundImage.png"]];

编辑:

据我所知UITableViewCell,在这种常见情况下选择后无法突出显示:

  1. 有一个地方设置cell.selectionStyle = UITableViewCellSelectionStyleNone;
  2. 您实施willSelectRowAtIndexPath并返回nil
  3. 有设置[self.tableView setAllowsSelection:NO];
  4. 可能是你设置的self.tableView.userInteractionEnabled = NO;或cell.userInteractionEnabled = NO
  5. 您将 UITableViewCell 子类化并实现了不正确的此类方法setSelected:animated:setHighlighted:animated

可能会分享您的cellForRowAtIndexPath方法代码来调查问题

于 2013-08-06T17:37:31.570 回答
0

您可以通过多种方式更改突出显示颜色。

更改selectionStyle单元格的属性。如果将其更改为UITableViewCellSelectionStyleGray,它将是灰色的。

你也可以在你的tableView:didSelectRowAtIndexPath:

//do something like this for color
cell.selectionStyle= UITableViewCellSelectionStyleGray;

// for image
cell.selectedBackgroundView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"imageName"]];

更改selectedBackgroundView属性。实际上,创建蓝色渐变的是视图。您可以创建一个视图并绘制您喜欢的任何内容,并将该视图用作表格视图单元格的背景。

祝你好运。。

于 2013-08-06T17:42:52.433 回答