ios中tableview的移动添加删除

//
//  MJViewController.m
//  UITableView-编辑模式
//
//  Created by mj on 13-4-11.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "MJViewController.h"

@interface MJViewController () {
    // 当前的编辑模式
    UITableViewCellEditingStyle _editingStyle;
}
@property (nonatomic, retain) NSMutableArray *data;
@end

@implementation MJViewController
#pragma mark - 生命周期方法
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.data = [NSMutableArray array];
    
    for (int i = 0; i<20; i++) {
        NSString *text = [NSString stringWithFormat:@"mj-%i", i];
        [self.data addObject:text];
    }
    
    // 设置tableView可不可以选中
    //self.tableView.allowsSelection = NO;

    // 允许tableview多选
    //self.tableView.allowsMultipleSelection = YES;

    // 编辑模式下是否可以选中
    //self.tableView.allowsSelectionDuringEditing = NO;

    // 编辑模式下是否可以多选
    //self.tableView.allowsMultipleSelectionDuringEditing = YES;

    // 获取被选中的所有行
    // [self.tableView indexPathsForSelectedRows]

    // 获取当前可见的行
    // [self.tableView indexPathsForVisibleRows];
}

- (void)viewDidUnload {
    [super viewDidUnload];
    self.data = nil;
}

- (void)dealloc {
    [_data release];
    [super dealloc];
}

#pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"UITableViewCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier] autorelease];
        cell.detailTextLabel.text = @"详细描述";
    }
    
    cell.textLabel.text = [self.data objectAtIndex:indexPath.row];
    
    return cell;
}

#pragma mark - 代理方法
#pragma mark 设置Cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    NSLog(@"didSelectRowAtIndexPath");
//}

#pragma mark 提交编辑操作时会调用这个方法(删除,添加)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    // 删除操作
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // 1.删除数据
        [self.data removeObjectAtIndex:indexPath.row];
        
        // 2.更新UITableView UI界面
        // [tableView reloadData];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    } else {
        // 添加操作
        
        // 1.添加数据
        int row = indexPath.row + 1;
        [self.data insertObject:@"新添加的数据" atIndex:row];
        
        // 2.更新UI界面
        //[tableView reloadData];
        NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:0];
        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:path] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

#pragma mark 决定tableview的编辑模式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return _editingStyle;
}
#pragma mark 只有实现这个方法,编辑模式中才允许移动Cell
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    // NSLog(@"from(%i)-to(%i)", sourceIndexPath.row, destinationIndexPath.row);
    // 更换数据的顺序
    [self.data exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
}

#pragma mark - 公共方法
#pragma mark 删除数据
- (void)deleteData {
    _editingStyle = UITableViewCellEditingStyleDelete;
    
    // 开始编辑模式
    // self.tableView.editing = YES;
    // [self.tableView setEditing:YES];
    
    BOOL isEditing = self.tableView.isEditing;
    // 开启关闭编辑模式
    [self.tableView setEditing:!isEditing animated:YES];
}

#pragma mark 添加数据
- (void)addData {
    _editingStyle = UITableViewCellEditingStyleInsert;
    
    BOOL isEditing = self.tableView.isEditing;
    // 开启关闭编辑模式
    [self.tableView setEditing:!isEditing animated:YES];
}
@end
原文地址:https://www.cnblogs.com/gcb999/p/3349141.html