2012-11-21 37 views
1

我正在绘制自定义单元格,以实现我想要的某种外观。但是,我想根据单元格是否选择来执行不同的绘图。我真的不只是想要默认的颜色。自定义单元格选中

我改变内容的看法背景色在这个方法的观点:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 

然而,它只是没有正确显示,主要是它没有考虑到附带,只是有色,直到辅助指标。有没有更好的方法来实现这一点?

- (void)drawRect:(CGRect)rect 
{   
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    // Background 
    CGContextSetFillColorWithColor(context, CELL_BACKGROUND_COLOR); 
    CGContextMoveToPoint(context, 0.0f, 0.0f); 
    CGContextAddLineToPoint(context, rect.size.width, 0.0f); 
    CGContextAddLineToPoint(context, rect.size.width, rect.size.height); 
    CGContextAddLineToPoint(context, 0.0f, rect.size.height); 
    CGContextClosePath(context); 
    CGContextFillPath(context); 

    // Top line 
    CGContextSetStrokeColorWithColor(context, CELL_TOP_LINE_COLOR); 
    CGContextSetLineWidth(context, CELL_LINE_WIDTH); 
    CGContextSetLineCap(context, kCGLineCapSquare); 
    CGContextMoveToPoint(context, 0.0f, 0.0f); 
    CGContextAddLineToPoint(context, rect.size.width, 0.0f); 
    CGContextStrokePath(context); 

    //Bottom line 
    CGContextSetStrokeColorWithColor(context, CELL_BOTTOM_LINE_COLOR); 
    CGContextSetLineWidth(context, CELL_LINE_WIDTH); 
    CGContextSetLineCap(context, kCGLineCapSquare); 
    CGContextMoveToPoint(context, 0.0f, rect.size.height); 
    CGContextAddLineToPoint(context, rect.size.width, rect.size.height); 
    CGContextStrokePath(context); 
} 

回答

0

我发现了一个效果很好的解决方案,它还在默认配件视图下绘制,如附件指示器。我创建了自定义视图的BackgroundView和SelectedBackgroundView,我只是使用drawRect方法来创建自定义绘图。它工作得很好,表现似乎没问题。如果有人想看到完整的代码,请告诉我。

[cell setBackgroundView:[[BackgroundView alloc] init]]; 
[cell setSelectedBackgroundView:[[SelectedBackgroundView alloc] init]]; 
0

我想你应该修改drawRect方法,改变你的颜色是如何取决于isSelected属性的设置。当setSelected方法可能不会更改任何内容时,更改内容的视图背景,因为它将覆盖drawRect方法。

+0

我想那不过,当我切换屏幕时,电池将改变相同的颜色,我想不出一个有效的方式来处理这个 – Vikings

+0

你尝试通过强制选定单元格的重新划分任何机会 - 可能在你选择的单元格上有一个setNeedsDisplay。只是一个建议。 – tiguero

+0

是的,这种方法的问题是当你选择一个单元格时,你将手指放在你选择的位置,但是单元格不会突出显示,直到你让手指离开屏幕,不确定是否有方法 – Vikings

0

我使用这个:

if(self.highlighted || self.selected) { 
    //set text color 
    textColor = [UIColor colorWithRed:204.0/255.0 green:255.0/255.0 blue:0 alpha:1.0]; 

    //set background color 
    [[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.7] set]; 
    CGContextFillRect(context, r); 
} 

还要检查你的电池的selectionStyle财产。

+0

这是在drawRect方法吗? – Vikings

+0

drawRect在我的自定义单元格中。 – Alex

+0

而我假设你将selectionStyle设置为none,并在didSelectRow中调用setNeedsDisplay? – Vikings