2

我想设置一个单元格的 2 个完全不同的外观(和内容)。(一个被选中,一个未被选中)。

这段代码有效,但是,因为所选单元格的图像是 50% 透明的,我可以在底部看到 BackgroundView。

显然, selectedBackgroundView 提供了一种很好的方式来填充单元格,其中元素仅在被选中时可见。除了 BackgroundView 是否有其他位置来填充单元格,一个位置如何不会出现在 selectedBackgroundView 上

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  NSString *cellIdentifier = @"hello";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
  }


    UIImage *image = [UIImage imageNamed:@"listview_btn.png"];
    UIImage *imageThumb = [UIImage imageNamed:@"01_thumb.jpg"];
    UIImageView* imageView = [[UIImageView alloc] initWithImage:imageThumb];
    [imageView setFrame:CGRectMake(0, 0, 50, 67)];
    [cell setBackgroundView:[[UIImageView alloc]initWithImage:image] ];
    [cell.backgroundView addSubview:imageView];



    UIImage *imageSel = [UIImage imageNamed:@"listview_btn_on.png"];
    UIImage *imageThumbSel = [UIImage imageNamed:@"01_thumb_Sel.jpg"];
    UIImageView* imageViewSel = [[UIImageView alloc] initWithImage:imageThumbSel];
    [imageViewSel setFrame:CGRectMake(110, 0, 50, 67)];
    [cell setSelectedBackgroundView:[[UIImageView alloc]initWithImage:imageSel] ];
    [cell.selectedBackgroundView addSubview:imageViewSel];
4

1 回答 1

0

Make the selected background view opaque. Since it is always inserted above the background view, you can composite them in Photoshop or another graphics editor and use the result as the selected background view.

If you cannot afford this approach (I still highly recommend it), you can subclass UITableViewCell and override both setHighlighted:animated and setSelected:animated to set the background view property to nil when selected/highlighted and restore it when deselected/unhighlighted. However it is a much more involved approach, IMHO.

于 2012-04-28T05:00:39.840 回答