iOS給獨立的UITableview中添加側滑功能是很簡單的衙猪,UITableView的代理方法中已經集成了側滑刪除的功能柔昼,只要實現(xiàn)以下的方法就能增加側滑刪除哑芹。
//先要設Cell可編輯
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
//定義編輯樣式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
//修改編輯按鈕文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
return @"刪除";
}
//設置進入編輯狀態(tài)時,Cell不會縮進
- (BOOL)tableView: (UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
//點擊刪除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
//在這里實現(xiàn)刪除操作 //刪除數據岳锁,和刪除動畫
[self.myDataArr removeObjectAtIndex:deleteRow];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:deleteRow inSection:0]] withRowAnimation:UITableViewRowAnimationTop];
}
但是當UItableView嵌套在UIScrollView中時,由于手勢沖突蹦魔,會發(fā)現(xiàn)上述方法失效啦激率,這時可以使用以下方法:
//使用UITableViewRowAction來實現(xiàn)側滑
-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
UITableViewRowAction *actionName = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"yourAction" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
/* your code here*/
}];
return @[collectionRowAction];
}