您的位置:首页 > 产品设计 > UI/UE

UItableView编辑的步骤

2014-04-23 15:21 113 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iphone ios