UI之tableViewEdit(删除,添加,插入)

来源:互联网 发布:网络电视机不用机顶盒 编辑:程序博客网 时间:2024/06/07 23:46

  

     self.navigationItem.title = @"笑笑";

    self.tableView = [[UITableView alloc]initWithFrame: self.view.frame style:UITableViewStylePlain];
    //设置数据源代理
    self.tableView.dataSource = self;
    //设置代理
    self.tableView.delegate = self;
    self.tableView.backgroundColor = [UIColor yellowColor];
    self.tableView.separatorColor = [UIColor cyanColor];
    [self.view addSubview:self.tableView];

    NSMutableArray *group1 = [NSMutableArray arrayWithObjects:@"天亮了",@"明天,你好",@"God is a girl", nil];
    NSMutableArray *group2 = [NSMutableArray arrayWithObjects:@"流行",@"现代",@"DJ", nil];
    NSMutableArray *group3 = [NSMutableArray arrayWithObjects:@"民族",@"爵士",@"街舞", nil];
    self.array = [[NSMutableArray alloc]initWithObjects:group1,group2,group3, nil];
    //在导航栏右边添加编辑按钮
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    //在导航栏左边添加编辑完成按钮
    UIBarButtonItem *left = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(finish)];
    self.navigationItem.leftBarButtonItem = left;
}
#pragma mark-----UITableViewDataSource

//返回每个分区的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //取出对应分区下边小组
    NSMutableArray *group = self.array[section];
    return group.count;
}
//返回多少个分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.array.count;
}
//cell显示的位置及内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //创建一个静态全局标量,--重用标示符
    static NSString *identifier = @"Cycle";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    //设置背景颜色
    cell.backgroundColor = [UIColor magentaColor];
    //判断cell是否为空,如果为空表示重用队列里没有可用cell
    if (cell == nil) {
        //cell为空时重新创建
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    //取出当前分区的小组
    NSMutableArray *group = self.array[indexPath.section];
    cell.textLabel.text = group[indexPath.row];
    
    return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return @"music";
    }else if (section == 1){
    return @"song";
    }else if (section == 2){
        return @"dance";
    }
    return nil;
}
#pragma mark -----TableView可以编辑
- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
    //第一步:让当前的tableView处于可编辑状态
    [self.tableView setEditing:YES animated:YES];
}
//第二步:让所有cell处于可编辑状态
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
//第三步:选择tableView的编辑样式(添加或者删除)
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    //选择删除和添加时更换return返回内容,插入和添加都用UITableViewCellEditingStyleInsert
//    return UITableViewCellEditingStyleDelete;
    return UITableViewCellEditingStyleInsert;
}
//第四步:编辑完成,提交编辑状态(先操作数据源,后更新UI)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    //找到对应分区的
    NSMutableArray *group = self.array[indexPath.section];
    //delete
    //删除对应分区数组里对应位置的元素
    //[group removeObjectAtIndex:indexPath.row];
    ////通知tableView删除对应行
    //[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    //add
    //在tableView指定位置插入元素
    [group insertObject:@"你懂得" atIndex:indexPath.row];
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
- (void)finish{
    [self.tableView setEditing:NO animated:YES];
}

#pragma mark-----TableView移动方法
//第一步:让tableView处于可编辑状态
//第二步:指定tableView哪一行可以编辑
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;//让所有cell都可以移动
}
//第三步:移动完成
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    //获取元素所在数组
    NSMutableArray *array = self.array[sourceIndexPath.section];
    //把当前数组中对应位置的元素取出来,指向将要被删除的元素,如果继续访问可能有潜在的风险,添加retain持有这个对象一份
    NSString *name = [array[sourceIndexPath.row] retain];
    //把元素从原来位置上删除
    [array removeObjectAtIndex:sourceIndexPath.row];
    
    //插入到新的位置
    [array insertObject:name atIndex:destinationIndexPath.row];
    [name release];
}
//监测一定过程禁止跨区域移动
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{
    //判断起始位置的区域跟将要移动目的地区域是否为一个区域
    if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
        return proposedDestinationIndexPath;
    }//从哪来回哪去
    return sourceIndexPath;
}

1 0
原创粉丝点击