前段時間寫了一篇tableView多選刪除的文章http://www.reibang.com/p/abfc4d7e56a9吧秕,然后一直有人問怎么自定義多選狀態(tài)的按鈕樣式,系統(tǒng)默認(rèn)是藍(lán)色的:就像這樣
我們想實現(xiàn)右邊的藍(lán)色按鈕自定義樣式歪泳,我找了個橙色的圖片(當(dāng)然實現(xiàn)什么效果看你找什么樣的圖片),實現(xiàn)的效果是這樣的蛔外,
注意:如果只是想改變左邊選中顏色冀值,只要改變cell前景色就可以cell.tintColor = [UIColor orangeColor];
如果要替換選中的圖片在往下看姆怪,我的思路是這樣的:首先看這個選中狀態(tài)按鈕的層級關(guān)系
看到了層級關(guān)系嘿辟,這個選中狀態(tài)按鈕不是一個button而是一個UIImageView舆瘪,這個UIImageView在UITableViewCellEditControl上面,這個類沒見過红伦,但是看見了Control的后綴他一定繼承自UIControl,確定了他的層級關(guān)系淀衣,第一個想法是在初始化cell的方法中把選中圖片替換掉就可以了
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
for (id subCell in cell.subviews) {
if ([subCell isKindOfClass:[UIControl class]]) {
for (UIImageView *circleImage in [subCell subviews]) {
circleImage.image = [UIImage imageNamed:@"CellButtonSelected"];
}
}
}
但是試了之后發(fā)現(xiàn)不管用,不進(jìn)去最后設(shè)置image的方法昙读,想想這個選中圖片有兩種狀態(tài)選中和未選中狀態(tài),我要設(shè)置的是選中狀態(tài)的圖片膨桥,而cell初始化沒有imageView蛮浑,所以在在自定cell布局中寫,或者在didSelectRowAtIndexPath的方法中寫,先說在didSelectRowAtIndexPath的方法
NSArray *subviews = [[tableView cellForRowAtIndexPath:indexPath] subviews];
for (id subCell in subviews) {
if ([subCell isKindOfClass:[UIControl class]]) {
for (UIImageView *circleImage in [subCell subviews]) {
circleImage.image = [UIImage imageNamed:@"CellButtonSelected"];
}
}
}
點擊cell只嚣,管用沮稚!這種方式可以達(dá)到不想要cell的選中樣式的效果,平時你設(shè)置cell.selectionStyle = UITableViewCellSelectionStyleNone;這樣cell左邊的選中圓圈也不見了册舞,但是如果在didDeselectRowAtIndexPath中設(shè)置未選中圖片圓圈蕴掏,在didSelectRowAtIndexPath中設(shè)置選中圖片,左邊選中圓圈就顯示了调鲸。
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSArray *subviews = cell.subviews;
for (id subCell in subviews) {
if ([subCell isKindOfClass:[UIControl class]]) {
for (UIImageView *circleImage in [subCell subviews]) {
circleImage.image = [UIImage imageNamed:@"CellButton"];
}
}
}
效果是這樣的盛杰,沒有了cell的選中樣式
但是有一個問題就是全選的時候還是系統(tǒng)自帶的藍(lán)色圓圈,因為全選的方法是
for (int i = 0; i < self.dataArr.count; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionBottom];
}
沒有調(diào)用didSelectRowAtIndexPath方法藐石,所以在全選點擊后要替換每個藍(lán)色圓圈
for (int i = 0; i < self.dataArr.count; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionBottom];
NSArray *subviews = [[self.tableView cellForRowAtIndexPath:indexPath] subviews];
for (id subCell in subviews) {
if ([subCell isKindOfClass:[UIControl class]]) {
for (UIImageView *circleImage in [subCell subviews]) {
circleImage.image = [UIImage imageNamed:@"CellButtonSelected"];
}
}
}
}
還有一種簡單的方法在自定義的cell里面布局里面設(shè)置即供,就不用再在全選點擊事件中加入上面的代碼了:
-(void)layoutSubviews
{
for (UIControl *control in self.subviews){
if ([control isMemberOfClass:NSClassFromString(@"UITableViewCellEditControl")]){
for (UIView *view in control.subviews)
{
if ([view isKindOfClass: [UIImageView class]]) {
UIImageView *image=(UIImageView *)view;
if (self.selected) {
image.image=[UIImage imageNamed:@"CellButtonSelected"];
}
else
{
image.image=[UIImage imageNamed:@"CellButton"];
}
}
}
}
}
[super layoutSubviews];
}
但是這兩種方法都有一個問題,就是當(dāng)你長按一個按鈕時看到的還是系統(tǒng)的藍(lán)色圓圈于微,還要改變長按編輯的代碼
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
for (UIControl *control in self.subviews){
if ([control isMemberOfClass:NSClassFromString(@"UITableViewCellEditControl")]){
for (UIView *view in control.subviews)
{
if ([view isKindOfClass: [UIImageView class]]) {
UIImageView *image=(UIImageView *)view;
if (!self.selected) {
image.image=[UIImage imageNamed:@"CellButton"];
}
}
}
}
}
}
GitHub地址:https://github.com/D-james/MultipleSelectCell
希望可以幫到你