这次更加深入的使用UITableView,里面的内容使用的是Plist文件。
plist文件的建立方式如下new —>file—>Resource—->property List 输入名字回车就行了。
移动和插入,需要四个步骤
1.让tableView处于可编辑的状态
2.设置指定分区(section)中的行(row)是否可以被编辑
3.设置指定分区(section)中的行(row)是什么类型的编辑样式(插入/删除)
4.编辑完成(先造作数据,然后操作UI)
布局如下,在AppDelegate中使用导航控制器
建立一个City类。只有两个属性,一个name,一个area
一个FisrtTableViewController控制器,继承UITableViewController,不用设置代理,系统已经默认设置好了。要想进入下一级页面,直接更改这个类中的cell。
一个RootViewController控制器
AppDelegate代码
#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
_window.backgroundColor = [UIColor whiteColor];
RootViewController *rootVC = [[RootViewController alloc]init];
UINavigationController *navigationC = [[UINavigationController alloc]initWithRootViewController:rootVC];
_window.rootViewController = navigationC;
[_window makeKeyAndVisible];
[navigationC release];
[rootVC release];
[_window release];
// Override point for customization after application launch.
return YES;
}
RootViewController.m中的代码
#import "RootViewController.h"
#import "City.h"
#import "FirstTableViewController.h"
@interface RootViewController ()
@property(nonatomic,retain)UITableView * tableView;
//存放所有城市的对象
@property(nonatomic,retain)NSMutableDictionary * AllCityDictionary;
//存放所有key,如(北京,河南,山东,新疆)
@property(nonatomic,retain)NSMutableArray * keysArray;
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor grayColor];
//创建视图
self.tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
//设置代理
_tableView.dataSource = self;
_tableView.delegate = self;
[self.view addSubview:_tableView];
//读取plist文件
[self readPlist];
#pragma mark -------------设置编辑按钮
self.navigationItem.rightBarButtonItem = self.editButtonItem;
[_tableView release];
// Do any additional setup after loading the view.
}
#pragma mark ==============UITableView的编辑(删除,插入,移动)
#warning mark ----------------删除,插入
//1.让tableView处于可编辑的状态
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
//设置tableView的编辑状态
[_tableView setEditing:editing animated:animated];
}
//2.设置指定分区(section)中的行(row)是否可以被编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0&&indexPath.row == 0) {
return NO;
}
return YES;
}
//3.设置指定分区(section)中的行(row)是什么类型的编辑样式(插入/删除)
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 1) {
return UITableViewCellEditingStyleInsert;
}
return UITableViewCellEditingStyleDelete;
}
//4.编辑完成(先造作数据,然后操作UI)
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
//删除操作
//先操作数据源,
NSString *key = _keysArray[indexPath.section];
NSMutableArray *array = [_AllCityDictionary objectForKey:key];
[array removeObjectAtIndex:indexPath.row];
//然后UI
NSLog(@"%@",indexPath);
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
if (array.count == 0) {
//1.先操作数据
[_keysArray removeObject:key];
[_AllCityDictionary removeObjectForKey:key];
//2.操作UI
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:indexPath.section];
[tableView deleteSections:indexSet withRowAnimation: UITableViewRowAnimationRight];
}
}else if (editingStyle == UITableViewCellEditingStyleInsert){
//添加操作
//① 先操作数据
// 创建需要添加的数据对象
City *city = [[City alloc]init];
city.name = @"丰台";
city.area = @"40万";
//找到添加的位置
NSString *key = _keysArray[indexPath.section];
NSMutableArray *cityArray = _AllCityDictionary[key];
[cityArray insertObject:city atIndex:indexPath.row];
//②操作UI
[_tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
}
}
#pragma mark--------------UITableView移动
//1.让tableView处于可编辑状态(同上)
//2.设置指定的seciton中的行row是否可以被移动
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
//3.实现移动
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
//获取数组中要被移动的元素
//1.获取key
NSString *key = _keysArray[sourceIndexPath.section];
NSMutableArray *citysArray = _AllCityDictionary[key];
//3.获取你要移动的的那个城市对象
City *city = citysArray[sourceIndexPath.row];
//4. 给city的引用计数加1,为了防止野指针访问
[city retain];
//5.删除sourceIndex.row对应数组的城市对象
[citysArray removeObjectAtIndex:sourceIndexPath.row];
//6.把要移动的城市对象,移动到destinationIndexPath.row的那个位置
[citysArray insertObject:city atIndex:destinationIndexPath.row];
[city release];
}
-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{
//如果你最终移动到的位置和你现在的位置在同一个区
if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
NSLog(@"%lu",sourceIndexPath.section);
NSLog(@"%@",sourceIndexPath);
return proposedDestinationIndexPath;
}else{
return sourceIndexPath;
}
}
//当选中某一行的时候
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
FirstTableViewController *fistTVC = [[FirstTableViewController alloc]initWithStyle:UITableViewStylePlain];
NSString *key = _keysArray[indexPath.section];
fistTVC.city = [_AllCityDictionary objectForKey:key][indexPath.row];
[self.navigationController pushViewController:fistTVC animated:YES];
}
-(void)readPlist{
//获取plist文件所在的路径
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"citys" ofType:@"plist"];
//给keyArray开辟空间
self.keysArray = [NSMutableArray array];
//给字典开辟空间
self.AllCityDictionary = [NSMutableDictionary dictionary];
//读取plist
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:filePath];
//获取plist文件中有用的信息,并封装到Model对象中
for (NSString *key in dic) {
NSArray *arr = [dic objectForKey:key];
//此时arr存放的是城市的信息
//根据key创建每一个对应的数组,用来存放对应分组下的内容,例如:key是北京,此时需要创建一个数组array来存放海淀区,朝阳区,东城区,西城区····
NSMutableArray *cityArray = [NSMutableArray array];
for (NSDictionary *dictionary in arr) {
City *city = [[City alloc]init];
// city.name = [dictionary objectForKey:@"name"];
//
//
// city.area = [dictionary objectForKey:@"area"];
//相当于下面一句话
[city setValuesForKeysWithDictionary:dictionary];
//将modol对象添加到key对应的数组中
[cityArray addObject:city];
}
//内循环外面,外循环里面
[_AllCityDictionary setObject:cityArray forKey:key];
//将循环得到的每一个key,存到keysArray中
[_keysArray addObject:key];
}
//检查大字典_allCityDictionary有没有数据
NSLog(@"%@",_AllCityDictionary);
//如果数组中的字符串是汉字的话,使用compare:这个方法是不能排序的
[_keysArray sortUsingFunction:cityNameSort context:NULL];
}
NSInteger cityNameSort(id user1,id user2,void *p)
{
NSString * u1 = (NSString *)user1;
NSString *u2 = (NSString *)user2;
return [u1 localizedCompare:u2];
}
#pragma mark-----------实现协议中的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// NSArray *key = [_AllCityDictionary allKeys];
NSArray *array = [_AllCityDictionary objectForKey:_keysArray[section]];
return array.count;
//也可以用下面这个方法执行
// NSString *key1 = _AllCityDictionary.allKeys[section];
//
// return [[_AllCityDictionary objectForKey:key1] count];
}
//创建cell并设置cell上显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//根据重用标识,先到重用集合中去取有没有可用的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell==nil) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]autorelease];
}
//设置cell上显示的内容
// NSString *key = _AllCityDictionary.allKeys[indexPath.section];
NSString *key = _keysArray[indexPath.section];
City *city = [_AllCityDictionary objectForKey:key][indexPath.row];
cell.textLabel.text = city.name;
cell.detailTextLabel.text = city.area;
return cell;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
// NSString *key = _AllCityDictionary.allKeys[section];
NSString *key = _keysArray[section];
return key;
}
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return _keysArray;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return _AllCityDictionary.allKeys.count ;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#import "FirstTableViewController.h"
@interface FirstTableViewController ()
@end
@implementation FirstTableViewController
- (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;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
// Return the number of rows in the section.
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
// Configure the cell...
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
cell.textLabel.text = _city.name;
cell.detailTextLabel.text = _city.area;
return cell;
}