iOS-tableview的移动、添加、删除

@interface ViewController ()
{
    NSMutableArray *vcNrray;
    IBOutlet UITableView *vctableview;//tableview的xib
    
}
@end

@implementation ViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        vcNrray = [@[@"a",@"b",@"c",@"h",@"d",@"ds",@"sd",@"fd",@"gdd",@"ad"] mutableCopy];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [vctableview registerNib:[UINib nibWithNibName:@"VCTableViewCell" bundle:nil] forCellReuseIdentifier:@"abab"];
}

-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [vctableview setEditing:editing animated:animated];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return [vcNrray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    VCTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"abab" forIndexPath:indexPath];
    cell.cellLabel.text = vcNrray[indexPath.row];
    return cell;
    
}

#pragma mark -
#pragma mark - 移动
//- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return UITableViewCellEditingStyleNone;
//}
//
//- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return NO;
//}
//
//- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
//      toIndexPath:(NSIndexPath *)toIndexPath
//{
//    id object = [vcNrray objectAtIndex:fromIndexPath.row];
//    [vcNrray removeObjectAtIndex:fromIndexPath.row];
//    [vcNrray insertObject:object atIndex:toIndexPath.row];
//}
#pragma mark -
#pragma mark - 删除

//-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
//{
//
//
//    [vcNrray removeObjectAtIndex:indexPath.row];
//    [vctableview deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationAutomatic];
//
//}

#pragma mark -
#pragma mark - 添加
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleInsert;
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [vcNrray insertObject:@"111" atIndex:indexPath.row];
    [vctableview insertRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationAutomatic];
    
}

下载地址:http://code.cocoachina.com/view/131818

你可能感兴趣的:(iOS-tableview的移动、添加、删除)