1.UITableView补充 
1⃣️:-(void)p_data
@property (nonatomic,retain)NSMutableArray *dataArray;
@property (nonatomic,assign)UITableViewCellEditingStyle editStyle;
- (void)p_data{
    NSMutableArray *arr1 = @[@"范冰冰",@"宋慧乔",@"周迅"].mutableCopy;
    NSMutableArray *arr2 = @[@"bangbangbang",@"boomshakalaka",@"comeonboys",@"comeongirls"].mutableCopy;
    self.dataArray = [NSMutableArray array];
    [self.dataArray addObject:arr1];
    [self.dataArray addObject:arr2];
}
2⃣️:number
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
3⃣️:row
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.dataArray[section] count];
}
cell.textLabel.text = self.dataArray[indexPath.section][indexPath.row];
2.UITableView修改
1⃣️:让tableView处于编辑状态
- (void)p_navigation{
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"SET" style:UIBarButtonItemStyleDone target:self action:@selector(rightAction:)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"ADD" style:UIBarButtonItemStyleDone target:self action:@selector(leftAction:)];
}
- (void)leftAction:(UIBarButtonItem *)sender{
    self.editStyle = UITableViewCellEditingStyleInsert;
    [self.rv.tableView setEditing:!self.rv.tableView.editing animated:YES];
}
- (void)rightAction:(UIBarButtonItem *)sender{
    self.editStyle = UITableViewCellEditingStyleDelete;
    [self.rv.tableView setEditing:!self.rv.tableView.editing animated:YES];
}
2⃣️:指定可以编辑的行
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
3⃣️:指定tableView编辑的样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
        return self.editStyle;
}
4⃣️:完成编辑
①:修改数据流
1.[self.dataArray[indexPath.section] removeObjectAtIndex:indexPath.row];
2.[self.dataArray[indexPath.section] insertObject:@"New Peole" atIndex:indexPath.row + 1];
②:修改 UI
1[self.rv.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
2.NSIndexPath *newIndex = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
   [self.rv.tableView insertRowsAtIndexPaths:@[newIndex] withRowAnimation:UITableViewRowAnimationAutomatic];
③:完整代码:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
       if (self.editStyle == UITableViewCellEditingStyleDelete) {
         // 1.修改数据源
    [self.dataArray[indexPath.section] removeObjectAtIndex:indexPath.row];
    // 2.修改UI
    [self.rv.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
       }else if (self.editStyle == UITableViewCellEditingStyleInsert){
           // 添加
           [self.dataArray[indexPath.section] insertObject:@"New Peole" atIndex:indexPath.row + 1];
           NSIndexPath *newIndex = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
           [self.rv.tableView insertRowsAtIndexPaths:@[newIndex] withRowAnimation:UITableViewRowAnimationAutomatic];}
3.UItableView移动
1⃣️:让tableView处于编辑状态
同修改
2⃣️:制定可以移动的行
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
3⃣️:完成移动
①:修改数据源
// 拿出数据
    NSString *temp = self.dataArray[sourceIndexPath.section][sourceIndexPath.row];
    // 从数组中删掉
    [self.dataArray[sourceIndexPath.section] removeObjectAtIndex:sourceIndexPath.row];
    // 插入到指定的位置
    [self.dataArray[destinationIndexPath.section] insertObject:temp atIndex:destinationIndexPath.row];
②:修改UI
// 修改UI move方法
    [self.rv.tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
③:完整代码:
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    // 拿出数据
    NSString *temp = self.dataArray[sourceIndexPath.section][sourceIndexPath.row];
    // 从数组中删掉
    [self.dataArray[sourceIndexPath.section] removeObjectAtIndex:sourceIndexPath.row];
    // 插入到指定的位置
    [self.dataArray[destinationIndexPath.section] insertObject:temp atIndex:destinationIndexPath.row];
    // 修改UI move方法
    [self.rv.tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
}
④:不建议限制跨区域
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{
    if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
        return proposedDestinationIndexPath;
    }else {
        return sourceIndexPath;
    }
}
4.UITableViewController
1⃣️: 先注册
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
2⃣️:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.textLabel.text = @"123";
    return cell;
}
posted on 2015-11-21 17:00  sharkHZ  阅读(155)  评论(0编辑  收藏  举报