一 、UITableView左滑删除实现方式之一:
//进入编辑模式,按下出现的编辑按钮后,进行删除操作 iOS8必须实现这个代理方法才会有侧滑效果
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
}
-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
DeclareWeakSelf
UITableViewRowAction *deleteRoWAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
// 删除操作
[weakSelf.phonesTable beginUpdates];
LJPerson *person = nil;
LJSectionPerson *section = weakSelf.sortedPersons[indexPath.section];
person = section.persons[indexPath.row];
[[LJContactManager sharedInstance] deleteContactWithPerson:person];
if (section.persons.count > 2 ) {//删除row
NSMutableArray *sectionPersonsArray = [section.persons mutableCopy];
[sectionPersonsArray removeObjectAtIndex:indexPath.row];
section.persons = sectionPersonsArray; //清除数据源!
[weakSelf.phonesTable deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}else {//删除section
[weakSelf.sortedPersons removeObjectAtIndex:indexPath.section];
[weakSelf.keys removeObjectAtIndex:indexPath.section];
[weakSelf.phonesTable deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
}
[weakSelf.phonesTable endUpdates];
[weakSelf.phonesTable reloadData];
}];
return @[deleteRoWAction];
}
二 、UITableView左滑删除实现方式之二:
(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
//第二组可以左滑删除
if (indexPath.section == 2) {
return YES;
}
return NO;
}
// 定义编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
// 进入编辑模式,按下出现的编辑按钮后,进行删除操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
if (indexPath.section == 2) {
}
}
}
// 修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
return @"删除";
}
三 、UITableView左滑删除实现方式之三:
//iOS11 后的新方法,, 可以设置image和title
- ( UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
//删除
UIContextualAction *deleteRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"delete" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
[self.titleArr removeObjectAtIndex:indexPath.row];
completionHandler (YES);
[self.tableView reloadData];
}];
deleteRowAction.image = [UIImage imageNamed:@"删除"];
deleteRowAction.backgroundColor = [UIColor redColor];
UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction]];
return config;
}
注:::判断UITableView的滚动方向 And 判断UITableView是否滚动到了最底部
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView.contentSize.height > ScreenHeight) {
CGFloat height = scrollView.frame.size.height;
CGFloat contentOffsetY = scrollView.contentOffset.y;
CGFloat bottomOffset = scrollView.contentSize.height - contentOffsetY;
if (bottomOffset <= height){
//tableView滑动到最底部
}else {
//tableView没有滑动到最底部
}
//scrollView已经有拖拽手势,直接拿到scrollView的拖拽手势
CGPoint slide = [scrollView.panGestureRecognizer velocityInView:scrollView];
//获取到拖拽的速度 >0 向下拖动; <0 向上拖动 =0停止拖拽
if (slide.y > 0 ) {
//tableView向下拖动
}
}
}