UITableView编辑

来源:互联网 发布:淘宝黑搜索卡首页2017 编辑:程序博客网 时间:2024/06/05 17:20

在上一篇关于UITableView的方法属性的一些应用,只是一些基本运用,在很多时候,ios给我的感觉,在视觉上的效果,其对控件的使用远远超过那些基本方法,下面,我就来讲一下关于UITableView更多的编辑功能;

就好比我们用上一篇中的方法,创建了如下的一个通讯录效果:


我们可以看见右上角有编辑两个字,其实,它是一个按钮,它的作用是编辑对下面人员的增减的,也就是UITableViewCell中的内容进行设计,修改每一行,及row的内容;实现它的代码是:

在导航栏添加编辑按钮

 self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStyleDone target:self action:@selector(edtiAction:)];

其次,设定它的点击事件:

1,首先要让tableView处于编辑状态:

-(void)edtiAction:(id)sender{

    if(  _taVi.editing == NO){

    [_taVi setEditing:YES animated:YES];

    }else{

        [_taVi setEditing:NO animated:YES];

    }

}

2.设定row为可以编辑

// 能否编辑

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{

    NSLog(@"%d",1);

    return YES;

    

}

3.选择我们编辑时的样式

//编辑样式

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

    NSLog(@"%d",2);

//    return UITableViewCellEditingStyleNone;//无样式

//    return UITableViewCellEditingStyleDelete;//删除样式

//    return UITableViewCellEditingStyleInsert;//增加样式

    if (indexPath.row ==0) {               //可设定在处于编辑状态中,指定的行数为一种编辑状态模式

        return UITableViewCellEditingStyleDelete;

    }else{

        return UITableViewCellEditingStyleInsert;

    }

}


3.完成编辑,让前面的符号,拥有你想要给他们设定的功能,删除火增加;

//完成编辑

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

    NSLog(@"%d",3);

    if (editingStyle == UITableViewCellEditingStyleDelete) {

         [_tableArray removeObjectAtIndex:indexPath.row];//如果前面时减号,则删除这一条;

        [tableView reloadData];//刷新


    }else if(editingStyle == UITableViewCellEditingStyleInsert){//如果时加号,增加下面内容在下一行显示

    NSMutableDictionary *dic2 = [NSMutableDictionary dictionary];

    [dic2 setObject:@"1.png" forKey:@"imageName"];

    [dic2 setObject:@"小二" forKey:@"name"];

    [dic2 setObject:@"" forKey:@"sex"];

    [_tableArray addObject:dic2];

    

   

     [tableView reloadData];//刷新整个数据,

    }

}

3。在使用ios时,很多时候我们在进行编辑的时候,每一row的内容的youce有三条横线,它的意思时可移动,也就时说点击的一行,可以任意拖拽,插入任意一行,它的实现代码是:

//能够移动

-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{

    return YES;

}


4,在我们移动几行数据,比如网上移动或者下移,完成之后,拖拽整个cell,其内容冰不是移动完之后的那样了,会有覆盖的现象,这是因为你在移动的时候,只是移动了row,而存放内容的数组的下标并没有改变,所以,事实上,并没有改变,所以,我们要想让它得到完美的效果,我们在移动时,应该还许改变移动数据在数组中的位置:

//移动row

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{

    NSUInteger fromRow = [sourceIndexPath row];

    NSUInteger toRow = [destinationIndexPath row];

    

    id object = [[_tableArray objectAtIndex:fromRow]retain];

    NSLog(@"%d",[object retainCount]);

    [_tableArray removeObjectAtIndex:fromRow];

    [_tableArray insertObject:object atIndex:toRow];

    [tableView reloadData];

    [object release];

}





0 0
原创粉丝点击