天天看點

iOS小技巧-UITableView取消或設定cell的選中效果

UITableView的cell預設選中效果為:選中時cell變灰,有些時候不需要這個選中效果,或者需要其他的效果,以下介紹幾種方法:

1、方法一:可以通過更改UITableViewCell提供的selectionStyle屬性進行設定(貌似隻有none和default兩種style可用):

cell.selectionStyle = UITableViewCellSelectionStyleNone;
           

當然,這個屬性的其他值為

UITableViewCellSelectionStyleNone,//無色,有效

UITableViewCellSelectionStyleBlue,//藍色,沒效果

UITableViewCellSelectionStyleGray,//灰色,沒效果

UITableViewCellSelectionStyleDefault NS_ENUM_AVAILABLE_IOS(7_0)//預設

方法二:通過UITableView進行設定:

tableView.allowsSelection = NO;
           

2、自定義選中背景顔色

cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
cell.selectedBackgroundView.backgroundColor = [UIColor redColor];
           

繼續閱讀