// // ViewController.h // UI2_UITableViewDelete // // Created by zhangxueming on 15/7/14. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> @end
// // ViewController.m // UI2_UITableViewDelete // // Created by zhangxueming on 15/7/14. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import "ViewController.h" @interface ViewController () { UITableView *_tableView; NSMutableArray *_dataList; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain]; [self.view addSubview:_tableView]; //创建数据源 _dataList = [NSMutableArray array]; NSMutableArray *boys = [NSMutableArray array]; for (int i=0; i<10; i++) { NSString *name = [NSString stringWithFormat:@"boy%d", i]; [boys addObject:name]; } [_dataList addObject:boys]; NSMutableArray *grils = [NSMutableArray array]; for (int i=0; i<15; i++) { NSString *name = [NSString stringWithFormat:@"gril%d", i]; [grils addObject:name]; } [_dataList addObject:grils]; _tableView.delegate = self; _tableView.dataSource = self; //编辑状态 self.navigationItem.leftBarButtonItem = self.editButtonItem; } //设置表示图到编辑状态 - (void)setEditing:(BOOL)editing animated:(BOOL)animated { [super setEditing:editing animated:animated]; //让tableView处在编辑状态 [_tableView setEditing:editing animated:YES]; } #pragma mark ---UITableViewDataSource--- //返回分区的行数 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [[_dataList objectAtIndex:section] count]; } //返回分区的个数 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return _dataList.count; } //创建UITableViewCell - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *reuseIdentifier = @"cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier]; } NSArray *array = [_dataList objectAtIndex:indexPath.section]; cell.textLabel.text = [array objectAtIndex:indexPath.row]; cell.detailTextLabel.text = [NSString stringWithFormat:@"age:%d",arc4random()%40+10]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; cell.selectionStyle = UITableViewCellSelectionStyleGray; return cell; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { if (section==0) { return @"男孩"; } return @"女孩"; } #pragma mark ---删除插入--- //设置tableViewCell的删除按钮标题 - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath { return @"删除"; } //设置tableViewCell编辑状态风格 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { //return UITableViewCellEditingStyleDelete; return UITableViewCellEditingStyleInsert; } //执行插入,删除的动作 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if(editingStyle == UITableViewCellEditingStyleDelete) {//删除 [[_dataList objectAtIndex:indexPath.section] removeObjectAtIndex:indexPath.row]; //[_tableView reloadData]; [_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; } if (editingStyle == UITableViewCellEditingStyleInsert) {//插入 [[_dataList objectAtIndex:indexPath.section] insertObject:@"new" atIndex:indexPath.row]; //[_tableView reloadData]; [_tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; } } //移动 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { return YES; } //移动tableViewCell , 该方法必须实现 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { NSLog(@"--------"); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
转载于:https://www.cnblogs.com/0515offer/p/4645967.html