前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >UITableView 编辑状态(删除、添加、移动)

UITableView 编辑状态(删除、添加、移动)

作者头像
LeeCen
发布2018-10-11 17:01:07
1.4K0
发布2018-10-11 17:01:07
举报
文章被收录于专栏:LeeCenLeeCen
----- TableView 删除和添加 -----
代码语言:javascript
复制
   ** UITableView 编辑步骤
      1.让 tableView 处于编辑状态
      2.协议确定
          1)确定 cell 是否处于编辑状态
          2)设定 cell 的编辑样式(删除、添加)
          3) 编辑状态进行提交**
  • 开启编辑状态 //1.让 tableView 处于编辑状态 [tableView setEditing:YES animated:YES]; 如果没有开启编辑状态,没有左边的小红点

Paste_Image.png

  • 1)确定 cell 是否处于编辑状态
代码语言:javascript
复制
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
  return YES;
}
  • 2)设定 cell 的编辑样式(删除、添加)
代码语言:javascript
复制
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
        //插入
//    return UITableViewCellEditingStyleInsert;
    //删除
    return UITableViewCellEditingStyleDelete;
}
  • 3) 编辑状态进行提交
代码语言:javascript
复制
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (editingStyle) {
        case UITableViewCellEditingStyleNone:
        {
        }
            break;
        case UITableViewCellEditingStyleDelete:
        {
            //修改数据源,在刷新 tableView
            [_dataSource removeObjectAtIndex:indexPath.row];

            //让表视图删除对应的行
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }
            break;
        case UITableViewCellEditingStyleInsert:
        {
            [_dataSource insertObject:@"我是新增" atIndex:indexPath.row];
            //让表视图添加对应的行
            [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }
            break;

        default:
            break;
    }
}
  • 修改 Delete 文字
代码语言:javascript
复制
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

删除.gif

添加.gif

----- TableView 移动 -----

  • 1.实现协议:告诉 tableView 是否能够移动
代码语言:javascript
复制
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
  • 2.移动
代码语言:javascript
复制
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    //修改数据源
    [_dataSource exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
    //让表视图对应的行进行移动
    [tableView exchangeSubviewAtIndex:sourceIndexPath.row withSubviewAtIndex:destinationIndexPath.row];
}

移动.gif

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016.03.09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • ----- TableView 删除和添加 -----
  • ----- TableView 移动 -----
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档