在代理文件中定义属性,大概的思路是将TabBar作为一个根视图控制器,然后其中包含了两个视图控制器,一个是表视图,一个是组表视图,在表视图中通过实现协议的方法来完成每一行及行与行之间的联系
#import <UIKit/UIKit.h>
@class groupTableViewController;
@class liViewController;
@interface liAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) liViewController *viewController;
//添加一个导航栏控制器
@property (retain,nonatomic) UINavigationController *rootNavigationController;
//添加一个组表视图
@property (retain,nonatomic) groupTableViewController *groupTableVC;
//数组用于组建前边两个视图控制器,放在UITabBar里
@property (retain,nonatomic) NSArray *tabBarArray;
@end
在代理的.m文件中,完成对属性的初始化赋值和一些基本工作
#import "liAppDelegate.h"
#import "liViewController.h"
#import "groupTableViewController.h"
@implementation liAppDelegate
- (void)dealloc
{
[_tabBarArray release];
[_groupTableVC release];
[_rootNavigationController release];
[_window release];
[_viewController release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.viewController = [[[liViewController alloc] initWithNibName:@"liViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
//在根视图上添加一个导航栏
UINavigationController *tempNavigationController = [[UINavigationController alloc]initWithRootViewController:self.viewController];
//初始化一个组表视图
groupTableViewController *tempTableViewController = [[groupTableViewController alloc]initWithNibName:nil bundle:nil];
//为定义的两个属性赋值
self.rootNavigationController = tempNavigationController;
self.groupTableVC = tempTableViewController;
[tempTableViewController release];
[tempNavigationController release];
//初始化一个tabBar
UITabBarController *tabBarViewController = [[UITabBarController alloc]init];
//为数组赋值
self.tabBarArray = [NSArray arrayWithObjects:self.rootNavigationController,self.groupTableVC,nil];
//将前边两个视图控制器添加到tabBar里
tabBarViewController.viewControllers = self.tabBarArray;
self.window.rootViewController = tabBarViewController;
[tabBarViewController release];
[self.window makeKeyAndVisible];
return YES;
}
在第一个视图控制器的.h文件中
#import <UIKit/UIKit.h>
@interface liViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
@property(retain,nonatomic) UITableView *myTableView;
@property (retain,nonatomic) NSMutableArray *dataArray;
@end
#import "liViewController.h"
@interface liViewController ()
@end
@implementation liViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
{
//为导航栏添加标题
self.navigationItem.title = @"首页";
//为导航栏右边的barButton添加标题和关联动作
UIBarButtonItem *tempBarButton = [[UIBarButtonItem alloc]initWithTitle:@"BegEdit" style:UIBarButtonItemStylePlain target:self action:@selector(tableViewEdit:)];
self.navigationItem.rightBarButtonItem = tempBarButton;
[tempBarButton release];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//初始化数组
self.dataArray = [NSMutableArray arrayWithCapacity:5];
//为数组赋值
for (int i = 0; i < 5; i ++)
{
NSString *pStr = [NSString stringWithFormat:@"%d",i];
[self.dataArray addObject:pStr];
}
//为表视图赋值
UITableView *tempTableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
self.myTableView = tempTableView;
[tempTableView release];
// 为两个协议创建代理
self.myTableView.dataSource = self;
self.myTableView.delegate = self;
[self.view addSubview:self.myTableView];
}
//导航栏右button实现的动作
- (void) tableViewEdit:(id)sender
{
//为barBUtton添加标题
[sender setTitle:[self.myTableView isEditing]?@"BegEdit":@"EndEdit"];
//设置表视图是不是可编译状态
[self.myTableView setEditing:![self.myTableView isEditing]];
}
#pragma mark --- UITableViewDataSource---
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.dataArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.textLabel.text = [self.dataArray objectAtIndex:[indexPath row]];
return cell;
}
//设置当前的行是不是可以编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
//第一行不可以进行任何操作
if ([indexPath row] == 0)
{
return NO;
}
return YES;
}
//判断当前行可不可以移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
//不可以移动第三行
if ([indexPath row] == 2)
{
return NO;
}
return YES;
}
//删除或者添加一个新的行
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//判断当前行是不是可以进行删除
if (editingStyle == UITableViewCellEditingStyleDelete)
{
//删除掉要删除行的title
[self.dataArray removeObjectAtIndex:[indexPath row]];
//表视图开始更新
[self.myTableView beginUpdates];
//移除掉当前行的所有元素和控件
[self.myTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
//结束更新
[self.myTableView endUpdates];
}
//判断当前行是否可插入行
else if(editingStyle == UITableViewCellEditingStyleInsert)
{
//为要插入的行添加title
[self.dataArray insertObject:@"New cell" atIndex:[indexPath row]];
[self.myTableView beginUpdates];
//插入一个行所需要的所有控件个元素
[self.myTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
[self.myTableView endUpdates];
}
}
//设置当前行移动到某一位置
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
//将要被移动的行
NSInteger fromRow = [sourceIndexPath row];
//被移动行的目标位置
NSInteger toRow = [destinationIndexPath row];
//将要被移动行的标题
NSString *obj = [self.dataArray objectAtIndex:fromRow];
//把将要被移动的行的标题删除
[self.dataArray removeObjectAtIndex:fromRow];
//为目标位置的行添加标题
[self.dataArray insertObject:obj atIndex:toRow];
}
#pragma mark --- UITableViewDelegate ---
//设置当前的行可以进行哪种动作
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
//奇数行可以插入
if ([indexPath row] % 2)
{
return UITableViewCellEditingStyleInsert;
}
return UITableViewCellEditingStyleDelete;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) dealloc
{
[_dataArray release];
[_myTableView release];
[super dealloc];
}
@end
在第二个视图控制器的.m文件中#import <UIKit/UIKit.h>
@interface groupTableViewController : UIViewController<UITableViewDataSource>
@property (retain,nonatomic) UITableView *myTableiew;
//添加一个字典属性,用来存放plist文件中内容
@property (retain,nonatomic) NSDictionary *mydictionary;
//添加一个数字属性,用来存储字典中的所有key
@property (retain,nonatomic) NSArray *myArray;
@end
在其.m文件中#import "groupTableViewController.h"
@interface groupTableViewController ()
@end
@implementation groupTableViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//初始化所有的属性 记得style要为UITableViewStyleGrouped
UITableView *tempTableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
self.myTableiew = tempTableView;
[tempTableView release];
//找到plist文件
NSString *path = [[NSBundle mainBundle]pathForResource:@"data" ofType:@"plist"];
//为属性字典初始化并赋值
self.mydictionary = [NSDictionary dictionaryWithContentsOfFile:path];
//为数组赋值
self.myArray = [[self.mydictionary allKeys]sortedArrayUsingSelector:@selector(compare:)];
//设置代理
self.myTableiew.dataSource = self;
[self.view addSubview:self.myTableiew];
}
#pragma mark ---UITableViewDataSource---
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//[self.myArray objectAtIndex:section]是数组在索引值为section的对象,即第section段
//[self.mydictionary objectForKey:[self.myArray objectAtIndex:section]]因为数组中保存的是字典中的key,所以这段代码的意思是key为[self.myArray objectAtIndex:section]的value,因为字典中的对象是以数组的形式存储的,所以用count可以计算其长度
return [[self.mydictionary objectForKey:[self.myArray objectAtIndex:section]]count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
}
cell.textLabel.text = [[self.mydictionary objectForKey:[self.myArray objectAtIndex:[indexPath section]]]objectAtIndex:[indexPath row]];
return cell;
}
//返回有几段
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.myArray count];
}
//返回每段的title
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [self.myArray objectAtIndex:section];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return self.myArray;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void) dealloc
{
[_myArray release];
[_myTableiew release];
[_mydictionary release];
[super dealloc];
}
@end