ios-UI基础控件-UITableView 编辑移动删除的实现

2016-06-17  本文已影响635人  风一样的程序员
正义指引着我们

UITableView的编辑步骤

1.让tableView处于编辑状态
2.协议设定

代码示例

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
    self.editButtonItem.title = editing ? @"完成" : @"编辑";
}
//是否能编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
//设置编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return _editStyle;
}
 //提交编辑样式
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // 如果分组只剩一个元素,直接删除分组
        NSMutableArray *array = self.dataDictionary[self.dataArray[indexPath.section]];
        if (array.count == 1) {
            //从数据源中删除分组
            [self.dataDictionary removeObjectForKey:self.dataArray[indexPath.section]];
            //删除索引
            [self.dataArray removeObject:self.dataArray[indexPath.section]];
            //删除UI
            NSIndexSet *set = [NSIndexSet indexSetWithIndex:indexPath.section];
            [tableView deleteSections:set withRowAnimation:UITableViewRowAnimationFade];
        }else{
            //从数据源中删除元素
            [array removeObjectAtIndex:indexPath.row];
            //删除UI
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        }

    }else{
        NSDictionary *dict = @{@"name":@"侯兆国",@"gender":@"男",@"phoneNumber":@"120",@"headerImg":@"侯兆国"};
        MyClass *myClass = [MyClass myClassWithDic:dict];
        NSMutableArray *array = self.dataDictionary[self.dataArray[indexPath.section]];
        //插入数据index+1
        [array insertObject:myClass atIndex:indexPath.row+1];
        //修改UI
        NSIndexPath *index = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
        [tableView insertRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationFade];
    }
}

效果图:

示例图

UITableView的移动步骤

  1. 实现协议:告诉tableView是否能够移动
  1. 实现移动方法
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
    //根据原路径找到对应分组
    NSMutableArray *array = self.dataDictionary[self.dataArray[fromIndexPath.section]];
    //找到对应的内容
    MyClass *myClass = array[fromIndexPath.row];
    //删除原来的位置
    [array removeObject:myClass];
    //添加到新的位置
    [array insertObject:myClass atIndex:toIndexPath.row];
}

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
        return proposedDestinationIndexPath;
    }else{
        return sourceIndexPath;
    }
}
上一篇下一篇

猜你喜欢

热点阅读