1

我已经建立了一个UITableViewCell带有多个标签和图像的自定义,并且我(最终)让它看起来像我想要的样子。

但是,我似乎无法弄清楚如何让选择看起来像我想要的样子。我已经实现了该setSelected方法,它允许我很好地更改背景颜色,但我真正想要的是将背景颜色设置为黑色并在所选单元格的左侧显示一个彩色矩形(10 像素宽和单元格的高度)。

彩色框将以编程方式设置颜色,因此尽管我可以轻松地将 设置selectedBackgroundView为 a UIImageView,但在这种情况下这不起作用。

选择单元格时,以下代码将根本不显示selectedViewColor UIView

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    UIView *selectedView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.selectedBackgroundView.bounds.size.width, self.selectedBackgroundView.bounds.size.height)];
    [selectedView setBackgroundColor:[UIColor blackColor]];

    UIView *selectedView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, self.selectedBackgroundView.bounds.size.height)];
    [selectedViewColor setBackgroundColor:[UIColor redColor]];
    [selectedView addSubview:selectedViewColor];

    self.selectedBackgroundView = selectedView;

    [super setSelected:selected animated:animated];
}

这段代码看起来很基础,所以我认为在selectedBackgroundView.

任何替代方案或建议将不胜感激。

4

4 回答 4

4

There's a few things that could be done better with this code. Reinitialising two views in the setSelected method is pretty inefficient. You're actually blanking out everything in the cell when you select it with this code (which I'm guessing is not what you want). And finally, you're treating selectedBackgroundView as if it's the only view that gets displayed when you select the cell (according to Apple's documentation, it is displayed over the backgroundView).

Try the following (Edited) - Put this code where you create the cell (presumably, cellForRowAtIndexPath:)

UIView* container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cell.backgroundView.bounds.size.width, cell.backgroundView.bounds.size.height)]; // we need this because in cells, the background views always take up the maximum width, regardless of their frames.
container.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0]; // make it transparent - we only want the square subview to be seen.
UIView *selectedViewColor = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, self.selectedBackgroundView.bounds.size.height)];
[selectedViewColor setBackgroundColor:[UIColor redColor]];
[container addSubview:selectedViewColor]
cell.selectedBackgroundView = container;

This will make your red square appear when (and only when) the cell is selected, over the other views in the cell. From Apple's docs:

UITableViewCell adds the value of this property as a subview only when the cell is selected. It adds the selected background view as a subview directly above the background view (backgroundView) if it is not nil, or behind all other views.

Second, use the following code in your cell:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
   [super setSelected:selected animated:animated];
   if(selected == YES)
   {
      self.backgroundView.backgroundColor = [UIColor blackColor];
   }
   else
   {
      self.backgroundView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0] // replace this with whatever's appropriate - a return to the unselected state.
   }
}

This will ensure that your background turns black when the cell is selected (without otherwise interfering with what's displayed. Hopefully these changes should also resolve the issue you're having.

于 2012-09-25T01:56:58.150 回答
1

除了上面 Xono 的回答之外,跟进他对上面答案的评论:

在研究这个主题时我遇到过几次的一件事是 UITableViewCell 背后的代码实际上可能会在选择单元格时将所有子视图的背景颜色设置为透明。

如果您的单元格的 SelectionStyle 不是“无”,它确实会这样做。它在setHighlighted和的setSelected调用中都做到了UITableViewCell

我的第一个解决方案是继承 UITableViewCell 并覆盖这两个方法,而不是调用基类方法并做我自己的动画。这并不理想,因为您现在重新实现(可能很糟糕)标准 iOS 动画。

不过,进一步研究它,标准方法为整个视图的不透明度设置动画,而不是子视图(它们只设置子视图的颜色)。所以最好的方法是仍然调用基类方法,然后将你的子视图颜色重新设置回它们应该的颜色。即使您立即设置它们,因为它们的超级视图仍在为其不透明度设置动画,它仍然会正确淡入和淡出:

    public override void SetHighlighted(bool highlighted, bool animated)
    {
        base.SetHighlighted(highlighted, animated);
        SelectedBackgroundView.Subviews[0].BackgroundColor = SelectionColor;
    }

    public override void SetSelected(bool selected, bool animated)
    {
        base.SetSelected(selected, animated);
        SelectedBackgroundView.Subviews[0].BackgroundColor = SelectionColor;
    } 

这是 c# MonoTouch 代码,但它同样适用于 obj-c。
请注意,在我的情况下,我总是有 1 个子视图,因此硬编码 0 索引器。这可能因您的视图结构而异。

于 2013-01-26T01:30:30.560 回答
0

根据@Xono 的回答,我提出了这个问题的解决方案:

initWithStyle:reuseIdentifier:方法内部,我添加了一个分隔视图:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        CGRect selectionViewFrame = self.contentView.bounds;
        UIView *selectionView = [[UIView alloc] initWithFrame:selectionViewFrame];
        [selectionView setBackgroundColor:[UIColor colorWithWhite:1. alpha:0.65]];
        self.selectedBackgroundView = selectionView;        
        self.vwSelectedSeparator = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, 1.)];
        [self.vwSelectedSeparator setBackgroundColor:[UIColor colorWithHexString:@"aaa"]];
        [self.vwSelectedSeparator setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
        [selectionView addSubview:self.vwSelectedSeparator];

        [self setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
    }
    return self;
}

然后在setSelected:animated:方法中我添加了这些行:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    if (selected) {
        [self.vwSelectedSeparator setBackgroundColor:[UIColor colorWithHexString:@"aaa"]];
    }
}

对我来说,对 ios6 和 ios7 来说都是一种魅力。我希望这有帮助。

于 2014-03-21T21:18:12.273 回答
0

您可以为您的子视图覆盖方法“setBackgroundColor”并添加另一个具有相同功能的方法。在这种情况下,UITableCell 将无法在选择后更改 backgroundColor。

- (void)setBackgroundColor:(UIColor *)backgroundColor {
}

- (void)setColor:(UIColor *)color {
    [super setBackgroundColor:color];
}
于 2013-07-19T15:44:00.053 回答