// 主要代碼
第一步:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
第二步:
#pragma mark 在滑動手勢刪除某一行的時候,顯示出更多的按鈕
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 添加一個刪除按鈕
UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"刪除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"點(diǎn)擊了刪除");
// 1. 更新數(shù)據(jù)
//[_allDataArray removeObjectAtIndex:indexPath.row];
// 2. 更新UI
//[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}];
//這個可以用來刷新特定行
// [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
// 刪除一個置頂按鈕
UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置頂" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"點(diǎn)擊了置頂");
// 1. 更新數(shù)據(jù)
//[_allDataArray exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];
// 2. 更新UI
//NSIndexPath *firstIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
//[tableView moveRowAtIndexPath:indexPath toIndexPath:firstIndexPath];
}];
topRowAction.backgroundColor = [UIColor blueColor];
// 添加一個更多按鈕
UITableViewRowAction *moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"更多" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"點(diǎn)擊了更多");
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
}];
moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
// 將設(shè)置好的按鈕放到數(shù)組中返回
return @[deleteRowAction, topRowAction, moreRowAction];
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
editingStyle = UITableViewCellEditingStyleDelete;
}
第三步:
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
editingStyle = UITableViewCellEditingStyleDelete;
}
第四步:(如果需要更改 刪除或編輯 按鈕的高度 寬度的話,在cell中重寫此方法)
// 改變滑動刪除按鈕樣式
- (void)layoutSubviews {
[super layoutSubviews];
for (UIView *subView in self.subviews){
if([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {
CGRect cRect = subView.frame;
cRect.origin.y = self.contentView.frame.origin.y+10;
cRect.size.height = self.contentView.frame.size.height - 10;
subView.frame = cRect;
UIView *confirmView=(UIView *)[subView.subviews firstObject];
// 改背景顏色
confirmView.backgroundColor=[UIColor colorWithRed:254/255.0 green:85/255.0 blue:46/255.0 alpha:1];
for(UIView *sub in confirmView.subviews){
if([sub isKindOfClass:NSClassFromString(@"UIButtonLabel")]){
UILabel *deleteLabel=(UILabel *)sub;
// 改刪除按鈕的字體
deleteLabel.font=[UIFont boldSystemFontOfSize:15];
// 改刪除按鈕的文字
deleteLabel.text=@"刪除";
}
}
break;
}
}
}