UITableView编辑模式

来源:互联网 发布:linux c语言utf8转gbk 编辑:程序博客网 时间:2024/05/18 00:17
#pragma mark - 1.开启tableView的编辑状态, 实现按钮的点击方法// 添加 删除以及移动都需要开启tableView的编辑状态- (void)barButtonClick:(UIBarButtonItem *)barButton {    // 1. 开启tableView的编辑状态    // self.tableView.editing:是记录是否属于编辑状态    [self.tableView setEditing:!self.tableView.editing animated:YES];       // 2. 判断tableView是否处于编辑状态, 若处于编辑状态, button的标题改为完成    if (self.tableView.editing == YES) {        barButton.title = @"完成";    } else {        barButton.title = @"编辑";    }}

#pragma mark - UITableView 编辑#pragma mark 2.设置哪些分区的哪些行可以编辑// 如果不设置分区与行是否可变编辑, 默认是yes- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {    // 设置第0个分区的第一行不能编辑    if (indexPath.section == 0 && indexPath.section == 1) {        return NO;    } else        return YES;}

#pragma mark 3.指定某分区的添加删除样式- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {    if (indexPath.section == 0) {        if ([self.dataFristArray[indexPath.row] isEqualToString:@"添加"]) {            return UITableViewCellEditingStyleInsert;        }    } else if (indexPath.section == 1) {        if ([self.dataSecondArray[indexPath.row] isEqualToString:@"添加"]) {            return UITableViewCellEditingStyleInsert;        }    }    return UITableViewCellEditingStyleDelete;}

#pragma mark - 4.根据添加删除的样式, 对数组进行相应操作, 提交编辑- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {    // 需要对不同的分区, 用对应数组, 进行添加或者删除的操作    // 判断删除还是添加    if (editingStyle == UITableViewCellEditingStyleDelete) {               // 判断是那个分区的, 操作对应的数组        if (indexPath.section == 0) {            [self.dataFristArray removeObjectAtIndex:indexPath.row];            // 刷新界面 (删除时刷新界面)            // 传@[indexPath]这个数组: 支持多行删除            [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationAutomatic)];                   } else if (indexPath.section == 1) {            [self.dataSecondArray removeObjectAtIndex:indexPath.row];            // 刷新界面 (删除时刷新界面)            // 传@[indexPath]这个数组: 支持多行删除            [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationBottom)];        }    } else if (editingStyle == UITableViewCellEditingStyleInsert) {        if (indexPath.section == 0) {            // 插入数组            [self.dataFristArray insertObject:[NSString stringWithFormat:@"%ld", self.dataFristArray.count - 1] atIndex:indexPath.row + 1];            //如果想在插入按钮下面添加,则上面的indexPath.row要 + 1,再创建一个NSIndexPath新索引, 目的是插入数组的位置与刷新界面的位置相同            NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];                       [self.tableView insertRowsAtIndexPaths:@[newIndexPath]  withRowAnimation:(UITableViewRowAnimationFade)];        } else if (indexPath.section == 1) {             [self.dataSecondArray insertObject:[NSString stringWithFormat:@"%ld", self.dataSecondArray.count - 1] atIndex:indexPath.row];            [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationFade)];        }    }}

#pragma mark - tableView的移动#pragma mark 1. 指定某分区 某行可以移动- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {    // 可以指定特定的一行或一分区不可移动    if (indexPath.section == 0 && indexPath.row == 1) {        return NO;    }    return YES;}

#pragma mark 移动完成, 更改数据, 刷新页面- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {    //sourceIndexPath: 被移动的cell的索引也就是来源的索引(拿起的cell的索引)    //destinationIndexPath: 目的地的索引 (放下cell的位置的索引)       // 判断来源是第0分区    if (sourceIndexPath.section == 0) {               // 1. 利用来源索引保存数组中的数据        NSString *str = self.dataFristArray[sourceIndexPath.row];               // 2. 把数组中来源的索引的数据在数组中删除        [self.dataFristArray removeObjectAtIndex:sourceIndexPath.row];               // 3. 插入到目的地索引位置        [self.dataFristArray insertObject:str atIndex:destinationIndexPath.row];    } else if (sourceIndexPath.section == 1) {        NSString *str = self.dataSecondArray[sourceIndexPath.row];               [self.dataSecondArray removeObjectAtIndex:sourceIndexPath.row];               [self.dataSecondArray insertObject:str atIndex:destinationIndexPath.row];    }       // 整体刷新tableView    [self.tableView reloadData];}

#pragma mark 检测cell区域移动的方法- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath {    // sourceIndexPath 来源的索引    // proposedDestinationIndexPath 建议去处的索引(目的地索引)       // 判断来源分区和目的分区是否是同一分区, 若是, 返回目的索引, 否则返回来源索引    if (sourceIndexPath.section == proposedDestinationIndexPath.section) {        return proposedDestinationIndexPath;    } else {        return sourceIndexPath;    }}

0 0