UI10_TableView的编辑

来源:互联网 发布:淘宝上办学历是真的吗 编辑:程序博客网 时间:2024/06/08 07:30
我们先铺一个tableView然后将tableView设置成属性.另外我们在进行初始化的方法的时候会用到一个数组arr为了方便我们将其设置成属性.至于上面方法的内容可以寻找前面的内容有详写

1.添加编辑按钮edit和done

self,navigationItem.rightBarButtonItem=self.editButtonItem;

2.直接打开tableView的可编程模式

[self.tableView setEditing:YES animated:YES];

3.重写系统的编辑按钮点击触发的方法

-(void)setEditing:(BOOL)editing animated:(BOOL)animated{  [super setEditing:editing animated:animated];  [self.tableView setEditing:editing animated:YES];}

4.设置哪些行可以进行编辑

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{    //奇数行可以编辑,偶数行不可以编辑    //默认是yes:yes代表可以编辑   从0开始算得    if (indexPath.row%2==0) {        return NO;    }else{        return YES;    }}

5.UITableViewCellEditingStyle是个枚举.有两种模式一个是插入,一个是删除

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{    return UITableViewCellEditingStyleDelete;}

6.删除数据

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{    if (editingStyle==UITableViewCellEditingStyleDelete) {        //先删除数据源        [self.arr removeObjectAtIndex:indexPath.row];        //[self.tableView reloadData];//重新加载数据,将原来的数据重新走一遍        //通过tableview来删除上面的cell        //第一个参数:指定删除哪一个分区的哪个行,把他作为一个元素放在数组里        //第二个参数:删除的动画        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];    }}

7.修改删除按钮的标题

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

8.指定和删除
这个方法是iOS8.0之后出现的方法,可以在编辑状态的时候有多个按钮

-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{    UITableViewRowAction *deleteAction=[UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {        //按钮的点击所要触发的事件,都是写在block中        NSLog(@"触发了删除按钮");    }];    deleteAction.backgroundColor=[UIColor blueColor];    UITableViewRowAction *topAction=[UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {            }];    return @[deleteAction,topAction];}

9.移动

第一个参数:起始位置第二个参数:目的位置:移动到位置-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{    //先获取到起始位置的数据    NSString *str=[self.arr[sourceIndexPath.row]retain];    //2.把起始位置的对象从数据源中删除    [self.arr removeObjectAtIndex:sourceIndexPath.row];    //3.把数据插入到数组的目的位置上去    [self.arr insertObject:str atIndex:destinationIndexPath.row];    //存在一些问题:内存方面的  retain对对象进行加一然后最后release    [str release];}

10 tableView创建注意问题

UITableViewStylePlain的构成主要有:表头部/表底部、表内容 ---不会出现空出来一部分UITableViewStyleGrouped可以分为多个组。它的构成主要有:表头部/表底部、组头部/组底部、内容 --- 一般用这个有的时候会让cell不是从顶部开始  而是在顶部空出来一部分  

11 关于滑动问题

1.tableView.scrollEnabled  = NO;设置其不可滑动     2.tableView.showsHorizontalScrollIndicator/showsVerticalScrollIndicator= YES;//显示横向/纵向滑杆3.当tableview上下滑动时,获取cell的indexpath值-(void)scrollViewDidScroll:(UIScrollView *)scrollView{    NSIndexPath *path = [table indexPathForRowAtPoint:CGPointMake(scrollView.contentOffset.x,scrollView.contentOffset.y)];    NSLog(@"%li",path.row);}
0 0
原创粉丝点击