#import "AppDelegate.h"
#import "RootViewController.h"
#import "TableViewController.h"
#import "TwoTableViewController.h"
#import "TestTableViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// RootViewController *rootVC = [RootViewController new];
// UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:rootVC];
// self.window.rootViewController = navC;
// TableViewController *tableVC = [[TableViewController alloc]init];
// UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:tableVC];
// self.window.rootViewController = navC;
TwoTableViewController *twoVC = [TwoTableViewController new];
UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:twoVC];
self.window.rootViewController = navC;
// TestTableViewController *testVC = [TestTableViewController new];
// UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:testVC];
// self.window.rootViewController = navC;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
#import "RootViewController.h"
@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)NSMutableArray *allDataMutableArray; //存储数据
@end
@implementation RootViewController
//懒加载
-(NSMutableArray *)allDataMutableArray{
if (!_allDataMutableArray) {
_allDataMutableArray = [NSMutableArray new];
}
return _allDataMutableArray;
}
//造假数据
-(void)createData{
[self.allDataMutableArray addObject:@"HanOBa"];
[self.allDataMutableArray addObject:@"Hello"];
[self.allDataMutableArray addObject:@"World"];
[self.allDataMutableArray addObject:@"添加一行"];
}
- (void)viewDidLoad {
[super viewDidLoad];
//表视图编辑
self.navigationItem.title = @"表视图编辑";
//新建一个表视图和屏幕大小一致
UITableView *myTableView = [[UITableView alloc]initWithFrame:self.view.bounds];
myTableView.tag = 1000;
//指定代理并实现必须实现的代理方法
myTableView.delegate = self;
myTableView.dataSource = self;
//将表视图添加到父视图上
[self.view addSubview:myTableView];
//注册单元格
[myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"];
//调用假数据
[self createData];
//系统提供的一个编辑按钮(右按钮--删除)
self.navigationItem.rightBarButtonItem = self.editButtonItem;
//自己编辑一个左按钮(左按钮--刷新)
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(leftAction)];
}
//实现左按钮的方法:
-(void)leftAction{
UITableView *tableView = (UITableView *)[self.view viewWithTag:1000];
//刷新tableView上面的数据,实际上就是让实现的代理方法重新执行一次
[tableView reloadData];
}
#pragma mark - 必须实现的方法!!! 分区的行数和分区数不能给死,要给数据值,变量!!!
//定义单元格
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//从重用队列中获取一个cell,注册单元格时候给的重用标示符是什么,这里的重用标示符就是什么!!!
UITableViewCell *mycell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];
mycell.textLabel.text = self.allDataMutableArray[indexPath.row];
return mycell;
}
//每个分区的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.allDataMutableArray.count;
}
#pragma mark - 编辑、修改等的代理方法
//系统提供的开启当前视图控制器的编辑模式
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
[super setEditing:editing animated:animated];
//将表示图的编辑状态开启
UITableView *myTableView = (UITableView *)[self.view viewWithTag:1000];
[myTableView setEditing:editing animated:animated];
}
//设置编辑样式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
//编辑,插入
//return UITableViewCellEditingStyleInsert;
//删除
//return UITableViewCellEditingStyleDelete;