UItableView编辑的步骤

来源:互联网 发布:飞行员和程序员 编辑:程序博客网 时间:2024/04/29 22:22
self.navigationItem.rightBarButtonItem = self.editButtonItem;

第一步设置可编辑条件

- (void)setEditing:(BOOL)editing animated:(BOOL)animated{    // 调用父类的(使其可以切换)    [super setEditing:editing animated:YES];    // 获取表表视图的类型(定义成实例变量)    UITableView *tableView = (UITableView *)self.view;    // 设置可编辑状态    [tableView setEditing:editing animated:animated];}

第二部设置进入编辑状态

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{    return YES;}

第三部设置哪一个可以编辑

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{    if (indexPath.section == 0) {        return UITableViewCellEditingStyleDelete;    }if (indexPath.section == 1) {        return UITableViewCellEditingStyleInsert;    }else        return UITableViewCellEditingStyleDelete;}

第四部设置可编辑状态的样式

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{    return @"删除";}

第五部删除按钮标签名

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{        if (editingStyle == UITableViewCellEditingStyleDelete) {        // 删除数据        // 拿到数据对象        NSString *key = [self.groupNames objectAtIndex:indexPath.section];        NSMutableArray *arry = [self.personDic objectForKey:key];                // 把删除,插入都写到begin和end中        [tableView beginUpdates];        if ([arry count] > 1 ) {            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];            [arry removeObjectAtIndex:indexPath.row];        }else        {            // 删除字典中的数据            [self.personDic removeObjectForKey:key];            // 删分区时分区数量也得相应的减一            [self.groupNames removeObjectAtIndex:indexPath.section];            [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationRight];        }        [tableView endUpdates];                }else if (editingStyle == UITableViewCellEditingStyleInsert)    {            // 添加到指定位置    // NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];        Person *p = [Person personWithName:@"张三" sex:@"男" phoneNumber:@"32321323" address:@"大学西路" email:@"43454656@qq.com" age:@"32"];        // 通过key找出对应的数组信息        NSString *key = [self.groupNames objectAtIndex:indexPath.section];        // 通过键值找到对应的对象        NSMutableArray *arry = [self.personDic objectForKey:key];        // 在对应的数组中添加需要填加的对象        [arry insertObject:p atIndex:indexPath.row];        // 添加到当前行        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationRight];    }}
第六步进行具体的编辑操作




下面介绍一下如何移动数据(数据存储是通过字典的方式存储的)步骤如下

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{    return YES;}- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{    NSLog(@"%@", sourceIndexPath);    // 获取移动数据对象    NSString *sourceKey = [self.groupNames objectAtIndex:sourceIndexPath.section];    NSMutableArray *sourceGrop = [self.personDic objectForKey:sourceKey];    // 拿到数据对象    Person *p = [sourceGrop objectAtIndex:sourceIndexPath.row];    // 计数器加一次    [p retain];    // 拿到移动位置的数据位置    NSString *destinationkey = [self.groupNames objectAtIndex:destinationIndexPath.section];    // 拿到移动数据所在的数组    NSMutableArray *destinationkeyGrop = [self.personDic objectForKey:destinationkey];    // 移除原先的数据    [sourceGrop removeObject:p];    // 插入移动的后所在位置的数据    [destinationkeyGrop insertObject:p atIndex:destinationIndexPath.row];    // 释放数据    [p release];}
// 分区移动- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{    // 若果时同一个分区则交换    if (sourceIndexPath.section == proposedDestinationIndexPath.section) {        return proposedDestinationIndexPath;    }else    // 不在同一个分区则不移动       return sourceIndexPath;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 玩3d游戏头晕恶心怎么办 win10打cf没声音怎么办 英雄联盟玩家尚未准备就绪怎么办 玩手机想吐应该怎么办 玩手机多了头晕怎么办 玩cf老是无响应怎么办 玩穿越火线好卡怎么办 绝地求生画质卡顿怎么办 手机热点玩lol卡怎么办 一加6直播触手黑屏怎么办 ipad应用商店密码忘记了怎么办 爱派忘记了密码怎么办 爱派id密码忘了怎么办 爱派密码忘了怎么办 爱派的密码忘了怎么办 苹果爱派密码忘了怎么办 鼠标无法识别的usb设备怎么办 电脑鼠标无法识别usb设备怎么办 win7电脑用户密码忘了怎么办 联想win7旗舰版开不了机怎么办 驱动都被卸载了怎么办 电脑密码忘了怎么办w7旗舰版 笔记本电脑密码忘了怎么办w7 windows一键还原了怎么办 戴尔笔记本电脑键盘没反应怎么办 win10电脑系统盘满了怎么办 win7玩dnf卡死怎么办 cf老是卡住闪退怎么办 w7系统帐户被停用怎么办 海康硬盘录像机密码忘了怎么办 电信合约套餐到期后怎么办 电脑更新系统卡住了怎么办 格力空调显示e1怎么办 ae崩溃了没保存怎么办 电脑下面的状态栏没了怎么办 游戏32位不兼容怎么办 电脑开机dos红屏怎么办 win7进入dos红屏怎么办 手机玩游戏屏幕卡住不动怎么办 魔域英文版换中文版怎么办 党员培养期不足一年怎么办