123批量添加和删除单元格(扩展知识:设置单元格的尺寸和颜色)

效果如下:

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-08-25 01:36:51

123批量添加和删除单元格(扩展知识:设置单元格的尺寸和颜色)的相关文章

批量添加或删除用户

========================================================================================== #!/bin/bash # if [ ! $# -eq 1 ];then echo './user.sh --add|--del' exit 1 fi case "$1" in --add) for i in {1..10} do if id user$i &>/dev/null ;then

Windows批量添加和删除IP

随着天气变冷了,好多小伙伴都开始变懒了,都想用最快的方式完成任务 下面给大家介绍一下Windows批量添加和删除IP的办法 (1)批量添加IP 直接在CMD下边运行下边命令. for /l %i in (2,1,254) do netsh interface ip add address "本地连接" 192.168.1.%i 255.255.255.0for /l %i in (2,1,62) do netsh interface ip add address "本地连接&

SqlServer——批量操作(批量添加,删除)

批量添加数据: 一条insert语句批量插入多条记录 常见的insert语句,向数据库中,一条语句只能插入一条数据: insert into persons (id_p, lastname , firstName, city ) values(204,'haha' , 'deng' , 'shenzhen'); (如上,仅插入了一条记录) 怎样一次insert插入多条记录呢? 使用示例: insert into persons (id_p, lastname , firstName, city 

react.js 之 批量添加与删除功能

最近做的CMS需要用到批量添加图片的功能:在添加文件的容器盒子内,有两个内容,分别是:添加按钮与被添加的选择文件组件. 结构分析: 被添加的组件,我们称为:UploadQiNiuFiles(七牛文件上传组件),含一个删除当前组件的删除按钮 添加按钮的事件 被添加组件存放的容器 代码分析: 添加组件存放的容器:<div className="divBorder"> <div className="divBorder"> {addToBtn} /

NPOI 生成Excel (单元格合并、设置单元格样式:字段,颜色、设置单元格为下拉框并限制输入值、设置单元格只能输入数字等)

NPIO源码地址:https://github.com/tonyqus/npoi NPIO使用参考:源码中的 NPOITest项目 下面代码包括: 1.包含多个Sheet的Excel 2.单元格合并 3.设置单元格样式:字段,颜色 4.设置单元格为下拉框并限制输入值 5.设置单元格只能输入数字 // // GET: /Excel/ public ActionResult Write() { var workbook = new HSSFWorkbook();//从流内容创建Workbook对象

MyBatis批量添加和删除

p.MsoNormal,li.MsoNormal,div.MsoNormal { margin: 0cm; margin-bottom: .0001pt; text-align: justify; font-size: 10.5pt; font-family: 等线 } h1 { margin-right: 0cm; margin-left: 0cm; font-size: 24.0pt; font-family: 宋体; font-weight: bold } p { margin-right

vim编辑器批量添加和删除注释

添加注释: 方法一: (1)按Control+v(win下面ctrl+q)进入列模式: (2)按大些"I"进入插入模式,输入注释符"#"或者是"//",然后立刻按下ESC(两下) 方法二:替换命令 :起始行号,结束行号s/^/注释符/g 删除注释: 方法一: Ctrl + v 进入块选择模式,选中你要删除的行首的注释符号,注意// 要选中两个,选好之后按d即可删除注释 方法二: :起始行号,结束行号s/^注释符//g 参考文档: http://b

如何批量添加和删除SMTP地址

为用户添加一个SMTP地址, Set-Mailbox "Dan Jump" -EmailAddresses @{add="[email protected]"} 为用户添加多个SMTP地址: Set-Mailbox "Dan Jump" -EmailAddresses @{add="[email protected]","[email protected]"} 查看用户的SMTP地址: Get-Mailbo

Mybatis批量添加,删除与修改

1.批量添加元素session.insert(String string,object O) public void batchInsertStudent(){ List<Student> ls = new ArrayList<Student>(); for(int i = 5;i < 8;i++){ Student student = new Student(); student.setId(i); student.setName("maoyuanjun"