美文网首页
ios tableviewcell侧滑功能

ios tableviewcell侧滑功能

作者: coderJerry01 | 来源:发表于2017-03-13 15:01 被阅读1913次

先看效果图:


cell左滑效果图.jpg

不啰嗦,直接上代码:(iOS8.0还是iOS9.0后必须写的一个代理方法,不然无法实现左滑效果)

-  (void)tableView:(UITableView *)tableView
    commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
    forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
 
        }
}

当然还有这两个:

- (BOOL)tableView:(UITableView *)tableView
   canEditRowAtIndexPath:(NSIndexPath *)indexPath{
  return YES;
}
//
- (UITableViewCellEditingStyle)tableView:(UITableView*)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleDelete;
  }
 然后是添加多个操作:
//设置滑动时显示多个按钮
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
//添加一个删除按钮
UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleDestructive) title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
    RCSPhoneModel *phoneModel = self.blackListDatasource[indexPath.row];
    BOOL isDelete = [DbContactManager rcs_DeleteBlackListPhoneWithUUID:phoneModel.serverId phone:phoneModel.phone];
    if (isDelete) {
        [SVProgressHUD showSuccessWithStatus:@"删除成功"];
         [SVProgressHUD dismissWithDelay:1.f];
    }else{
        [SVProgressHUD showErrorWithStatus:@"删除失败"];
         [SVProgressHUD dismissWithDelay:1.f];
    }
    //1.更新数据
    [self.blackListDatasource removeObjectAtIndex:indexPath.row];
    //2.更新UI
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationAutomatic)];
}];
//删除按钮颜色
deleteAction.backgroundColor = [UIColor cyanColor];
//添加一个置顶按钮
UITableViewRowAction *topRowAction =[UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleDestructive) title:@"置顶"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
    
    //1.更新数据
    [self.blackListDatasource exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];
    //2.更新UI
    NSIndexPath *firstIndexPath =[NSIndexPath indexPathForRow:0 inSection:indexPath.section];
    [tableView moveRowAtIndexPath:indexPath toIndexPath:firstIndexPath];
//刷新此区数据
    NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:0];
    [tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
    //[tableView reloadData];
}];
//置顶按钮颜色
topRowAction.backgroundColor = [UIColor magentaColor];
//--------更多
UITableViewRowAction *moreRowAction = [UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleNormal) title:@"更多" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
}];
//背景特效
//moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:(UIBlurEffectStyleDark)];
//----------收藏
UITableViewRowAction *collectRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"收藏"handler:^(UITableViewRowAction *action,NSIndexPath *indexPath) {
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"收藏" message:@"收藏成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alertView show];
}];
//收藏按钮颜色
collectRowAction.backgroundColor = [UIColor greenColor];
//将设置好的按钮方到数组中返回
return @[deleteAction,topRowAction,moreRowAction,collectRowAction];
}

先这样,必要的解释之后再写,根据自己的需求实现!

相关文章

网友评论

      本文标题:ios tableviewcell侧滑功能

      本文链接:https://www.haomeiwen.com/subject/ztafnttx.html