有時候客戶會有一些特殊的要求,更改tableViewCell的側滑刪除按鈕的樣子就是其中之一,就像這樣:字體
這個效果其實也不難,只需在自定義的cell裏重寫layoutSubviews方法就好,具體代碼以下:3d
//修改刪除模式的樣式blog
-(void)layoutSubviews圖片
{it
[super layoutSubviews];io
for (UIView *subView in self.subviews)table
{iconfont
if([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")])方法
{im
UIView *confirmView=(UIView *)[subView.subviews firstObject];
//改背景顏色
confirmView.backgroundColor=[UIColor colorWithWhite:0.898 alpha:1.000];
for(UIView *sub in confirmView.subviews)
{
//修改字的大小、顏色,這個方法能夠修改文字樣式
/*
if ([sub isKindOfClass:NSClassFromString(@"UIButtonLabel")]) {
UILabel *deleteLabel=(UILabel *)sub;
deleteLabel.backgroundColor = [UIColor redColor];
//改刪除按鈕的字體大小
deleteLabel.font=[UIFont boldSystemFontOfSize:20];
//改刪除按鈕的文字
deleteLabel.text=@"嘿嘿";
}
*/
//添加圖片
if ([sub isKindOfClass:NSClassFromString(@"UIView")]) {
UIView *deleteView = sub;
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = [UIImage imageNamed:@"iconfont-zhuchang"];
imageView.frame = CGRectMake(CGRectGetMaxX(sub.frame) - 58, -5, 30, 30);
[deleteView addSubview:imageView];
}
}
break;
}
}
}
這樣你就成功的將那個紅色背景顏色的delete按鈕修改爲了一張自定義的圖片了。
不過細心的小夥伴必定會發現每次當你點擊按鈕的時候按鈕都會短暫的出現白色的delete文字,別怕,我們再重寫一個方法就好:
#pragma mark 更改刪除按鈕文字(使文字爲空)
-(NSString*)tableView:(UITableView*)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath*)indexpath {
return @" ";
}
這樣就解決了。
有時候也會有側滑顯示兩個選項的要求,這個也很簡單,在- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath;方法中添加UITableViewRowAction就行,具體的你們本身摸索一下就出來了。