TableView 是自帶滑動刪除這個功能骆膝,但是有時候并不能滿足我們的需求惶傻。例如暑中,需求要求刪除的那個按鈕可自定義壹瘟,這時候系統(tǒng)的功能就嗝屁了...
當然鲫剿,現(xiàn)在好用的輪子那么多,相信肯定有能滿足你要求的稻轨。今天灵莲,我來教大家怎么保持系統(tǒng)的滑動刪除功能不變,又可以隨意的自定義刪除按鈕殴俱,快捷方便政冻!
廢話不多說,先上圖:
開啟TableView 的滑動刪除功能
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"BtnClick_%zd",indexPath.row);
}-
自定義一個 UITableViewCell
實現(xiàn)如下方法:
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
UIView *deleteBgView = [UIView new];
deleteBgView.backgroundColor = [UIColor brownColor];
[self.contentView addSubview:deleteBgView];UIButton *deleteBtn = [UIButton new]; //deleteBtn.backgroundColor = [UIColor yellowColor]; [deleteBtn setImage:[UIImage imageNamed:@"delete"] forState:UIControlStateNormal]; [deleteBgView addSubview:deleteBtn]; [deleteBgView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.offset([UIScreen mainScreen].bounds.size.width); make.top.equalTo(self.contentView); make.bottom.equalTo(self.contentView).offset(1); make.width.equalTo(self.contentView); }]; [deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.width.offset(80); make.top.equalTo(deleteBgView); make.bottom.equalTo(deleteBgView); make.left.equalTo(deleteBgView); }]; } return self; }
這里我用了 Masonry 來做布局約束,相信大家一定不陌生线欲,到這里基本完成了明场,iOS7,8,9 親測無誤,輕松愉快李丰!
注: 這種做法只適用于一個按鈕的情況苦锨,多個按鈕的情況因為無法觸發(fā)按鈕的點擊事件,所以無從下手趴泌,研究出來的小伙伴舟舒,望告知!