效果如下:
ViewController.h
1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UITableViewController 4 @property (strong, nonatomic) NSMutableArray *mArrDataSource; 5 6 @end
ViewController.m
1 #import "ViewController.h" 2 3 @interface ViewController () 4 - (void)layoutUI; 5 - (void)tableViewSetting; 6 - (void)insertAll:(UIBarButtonItem *)sender; 7 - (void)deleteAll:(UIBarButtonItem *)sender; 8 @end 9 10 @implementation ViewController 11 12 - (void)viewDidLoad { 13 [super viewDidLoad]; 14 15 [self layoutUI]; 16 } 17 18 - (void)didReceiveMemoryWarning { 19 [super didReceiveMemoryWarning]; 20 // Dispose of any resources that can be recreated. 21 } 22 23 - (void)layoutUI { 24 _mArrDataSource = [[NSMutableArray alloc] initWithObjects:@"A", @"B", @"C", @"D", nil]; 25 26 self.navigationItem.title = @"批量添加和删除单元格"; 27 UIBarButtonItem *barBtnInsertAll = [[UIBarButtonItem alloc] initWithTitle:@"添加全部" 28 style:UIBarButtonItemStyleDone 29 target:self 30 action:@selector(insertAll:)]; 31 self.navigationItem.leftBarButtonItem = barBtnInsertAll; 32 self.navigationItem.leftBarButtonItem.enabled = NO; 33 34 UIBarButtonItem *barBtnDeleteAll = [[UIBarButtonItem alloc] initWithTitle:@"删除全部" 35 style:UIBarButtonItemStyleDone 36 target:self 37 action:@selector(deleteAll:)]; 38 self.navigationItem.rightBarButtonItem = barBtnDeleteAll; 39 40 [self tableViewSetting]; 41 } 42 43 - (void)tableViewSetting { 44 self.tableView.backgroundColor = [UIColor colorWithRed:0.962 green:0.991 blue:1.000 alpha:1.000]; //设置表格背景色 45 self.tableView.rowHeight = 50; //设置单元格高度 46 self.tableView.separatorColor = [UIColor redColor]; //设置单元格分割线;默认是gray灰色 47 self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; //设置单元格分割线样式;默认是UITableViewCellSeparatorStyleSingleLine 48 self.tableView.separatorInset = UIEdgeInsetsMake(5.0, 20.0, 5.0, 20.0); //设置单元格(上左下右)内边距 49 } 50 51 - (void)insertAll:(UIBarButtonItem *)sender { 52 //添加全部数据源数据 53 _mArrDataSource = [[NSMutableArray alloc] initWithArray:@[@"A", @"B", @"C", @"D"]]; 54 //添加全部单元格 55 NSUInteger len = _mArrDataSource.count; 56 NSMutableArray *mArrIndexPath = [[NSMutableArray alloc] initWithCapacity:len]; 57 for (NSUInteger i=0; i<len; i++) { 58 [mArrIndexPath addObject:[NSIndexPath indexPathForRow:i inSection:0]]; 59 } 60 [self.tableView insertRowsAtIndexPaths:mArrIndexPath 61 withRowAnimation:UITableViewRowAnimationAutomatic]; 62 63 self.navigationItem.rightBarButtonItem.enabled = YES; 64 sender.enabled = NO; 65 } 66 67 - (void)deleteAll:(UIBarButtonItem *)sender { 68 NSUInteger len = _mArrDataSource.count; 69 NSMutableArray *mArrIndexPath = [[NSMutableArray alloc] initWithCapacity:len]; 70 for (NSUInteger i=0; i<len; i++) { 71 [mArrIndexPath addObject:[NSIndexPath indexPathForRow:i inSection:0]]; 72 } 73 //删除全部数据源数据 74 [_mArrDataSource removeAllObjects]; 75 //删除全部单元格 76 [self.tableView deleteRowsAtIndexPaths:mArrIndexPath 77 withRowAnimation:UITableViewRowAnimationAutomatic]; 78 79 self.navigationItem.leftBarButtonItem.enabled = YES; 80 sender.enabled = NO; 81 } 82 83 #pragma mark - TableView 84 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 85 return @"列表"; 86 } 87 88 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 89 return 1; 90 } 91 92 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 93 return _mArrDataSource.count; 94 } 95 96 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 97 static NSString *cellIdentifier = @"cellIdentifier"; 98 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 99 if (!cell) { 100 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 101 } 102 cell.textLabel.text = _mArrDataSource[indexPath.row]; 103 cell.textLabel.textColor = [UIColor blackColor]; 104 cell.textLabel.textAlignment = NSTextAlignmentCenter; 105 cell.textLabel.font = [UIFont systemFontOfSize:22.0]; 106 107 cell.backgroundColor = [UIColor performSelector:NSSelectorFromString(indexPath.row % 2 == 0 ? @"grayColor" : @"lightGrayColor")]; 108 return cell; 109 } 110 111 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 112 113 } 114 115 @end
AppDelegate.h
1 #import <UIKit/UIKit.h> 2 3 @interface AppDelegate : UIResponder <UIApplicationDelegate> 4 @property (strong, nonatomic) UIWindow *window; 5 @property (strong, nonatomic) UINavigationController *navigationController; 6 7 @end
AppDelegate.m
1 #import "AppDelegate.h" 2 #import "ViewController.h" 3 4 @interface AppDelegate () 5 @end 6 7 @implementation AppDelegate 8 9 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 10 _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 11 ViewController *viewController = [[ViewController alloc] init]; 12 _navigationController = [[UINavigationController alloc] initWithRootViewController:viewController]; 13 _window.rootViewController = _navigationController; 14 //[_window addSubview:_navigationController.view]; //当_window.rootViewController关联时,这一句可有可无 15 [_window makeKeyAndVisible]; 16 return YES; 17 } 18 19 - (void)applicationWillResignActive:(UIApplication *)application { 20 } 21 22 - (void)applicationDidEnterBackground:(UIApplication *)application { 23 } 24 25 - (void)applicationWillEnterForeground:(UIApplication *)application { 26 } 27 28 - (void)applicationDidBecomeActive:(UIApplication *)application { 29 } 30 31 - (void)applicationWillTerminate:(UIApplication *)application { 32 } 33 34 @end
时间: 2024-11-20 13:45:20