UIView *bgView = [[UIView alloc]init];
bgView.backgroundColor = [UIColororangeColor];
self.selectedBackgroundView =bgView;
[bgView release];
该方法设置的是纯色, 也可以使用任何图片,把selectedBackgroundView设成UIImageView。
除了上面的方法,前几天发现了一个新方法:重写UITableViewCell 的 setSelected:animated:方法
-(void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
if (selected) {
self.backgroundColor = RGB(224,152, 40);
}
else {
self.backgroundColor = [UIColorclearColor];
}
}
点击Cell中的按钮时,如何取所在的Cell:
-(void)OnTouchBtnInCell:(UIButton*)btn
{
CGPoint point = btn.center;
point = [table convertPoint:pointfromView:btn.superview];
NSIndexPath* indexpath = [tableindexPathForRowAtPoint:point];
UITableViewCell *cell = [tablecellForRowAtIndexPath:indexpath];
...
// 也可以通过一路取btn的父窗口取到cell,但如果cell下通过好几层subview才到btn,就要取好几次 superview
// 所以我用上面的方法,比较通用。这种 方法也适用于其它控件。
}