您的位置:首页 > 产品设计 > UI/UE

UI - UITableView

2015-10-14 21:05 549 查看
<AppDelegate.m>

#import "AppDelegate.h"
#import "MainViewController.h"
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];

MainViewController *mainVC = [[MainViewController alloc ]init ];
UINavigationController *naVC = [[UINavigationController alloc]initWithRootViewController:mainVC];
self.window.rootViewController = naVC;

[mainVC release];
[naVC release];

return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end


<MainViewController.h>

#import <UIKit/UIKit.h>

@interface MainViewController : UIViewController

@end


<MainViewController.m>

#import "MainViewController.h"
#import "DetailViewController.h"

@interface MainViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,retain)NSMutableDictionary *groupDic;  //分组字典
@property (nonatomic,retain)NSMutableArray *allKeys;     //存储所有的 key 值

@end

@implementation MainViewController
- (void)dealloc
{
self.groupDic = nil;
self.allKeys = nil;
[super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

//布局 tableView
[self layoutTableView];

//获取数据
[self reloadData];

}
//获取数据
-(void)reloadData
{
//第一组
NSMutableArray *firstGroup = [NSMutableArray arrayWithObjects:@"asdf",@"fdas",@"qwer",@"qwer", nil];
//第二组
NSMutableArray *secondGroup = [NSMutableArray arrayWithObjects:@"123",@"345",@"567",@"789", nil];
//第三组
NSMutableArray *thirdGroup = [NSMutableArray arrayWithObjects:@"a",@"s",@"d",@"f",@"c",@"w" ,nil];
//第四组
NSMutableArray *fourthGroup = [NSMutableArray arrayWithObjects:@"1",@"2",@"4",@"3", nil];

//放入分数组
self.groupDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:firstGroup,@"第A组", secondGroup,@"第B组", thirdGroup,@"第C组" , fourthGroup,@"第D组",nil];
//获取所有 key 值
NSArray *keys = [_groupDic allKeys];
//将不可变数组转化为可变数组
self.allKeys = [NSMutableArray arrayWithArray:keys];

//排序
[_allKeys sortUsingSelector:@selector(compare:)];

}

//布局 tableView
-(void)layoutTableView
{
//1.创建对象   在创建 tableview 对象时需要给定 tableview 的风格,一旦创建 后期不可以修改
UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
//2.配置属性
tableView.backgroundColor = [UIColor yellowColor];
//设置数据源
tableView.dataSource = self;
//设置代理
tableView.delegate  = self;
//设置分割线颜色
tableView.separatorColor = [UIColor redColor];
//设置分割线风格
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
//设置行高     默认是44.0
tableView.rowHeight = 50;

//3.添加到父视图
[self.view addSubview:tableView];
//4.释放
[tableView release];

}

-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{

}
-(void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section
{

}

#pragma mark - tableViewDataScore And tableViewDelegate
//分区数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [_allKeys count];
}
//分区行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//获取对应分区的 key 值
NSString *key = [_allKeys objectAtIndex:section];
//获取分区对应数组
NSMutableArray *group = [_groupDic valueForKey:key];
//数组中的元素个数决定分区的行数
return [group count];
}

//分区的 header
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [_allKeys objectAtIndex:section];
}

//分区的 footer
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
return @"区尾";
}
//设置区头显示的 view
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
view.backgroundColor = [UIColor redColor];
return view;
}

//设置分区头高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 100;
}
//设置区尾 view
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *view = [[UIView alloc]init];
view.backgroundColor = [UIColor greenColor];
return view;
}
//设置区尾高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 50;
}

//cell点击事件  触发的方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//获取 key 值
NSString *key = [_allKeys objectAtIndex:indexPath.section];
//获取分组
NSMutableArray *group = [_groupDic valueForKey:key];
//获取对应名字
NSString *name = [group objectAtIndex:indexPath.row];
NSLog(@"=====%@=====",name);

//创建的下一个视图控制器
DetailViewController *detailVC = [[DetailViewController alloc ]init];
//传值
detailVC.string = name;
//push
[self.navigationController pushViewController:detailVC animated:YES];

[detailVC release];

}

//设置 cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
/* 对于 UITbleCell 对象创建也是需要给定风格样式,下面是几个常用的风格样式:
default : 没有 detailTextLable
subTitle : detailTextLable 位置在 textLable 下方
value1 : detailTextLable 位置在 textLale 右边
value2 : 没有 imageView
......
*/
//    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
//
//
//    //获取分区对应的 key 值
//    NSString *key  = [_allKeys objectAtIndex:indexPath.section];
//    //获取对应的数组
//    NSMutableArray *group = [_groupDic valueForKey:key];
//    //获取对应的人
//    NSString *name = [group objectAtIndex:indexPath.row];
//
//
//
//    //.imageView
//    cell.imageView.image = [UIImage imageNamed:@"夕阳.jpg"];
//    //.textLabel
//    cell.textLabel.text = name;
//    //.detailTextLabel
//    cell.detailTextLabel.text = @"详细小标题";
//    //辅助视图
//    cell.accessoryType = UITableViewCellAccessoryDetailButton;
//    //选中风格
//    cell.selectionStyle = UITableViewCellSelectionStyleDefault;
//

//cell 的重用
//设置静态变量变量标识符
static NSString *identifier = @"identifier";

//从 cell 的重用队列中取已经被 identifier 标识的 cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//如果 cell 为空,创建一个 cell
if (!cell) {
// 创建 cell,并为其添加 identifier 的标识
cell = [[UITableViewCell alloc ]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
}

//获取对应 key 值
NSString *key = [_allKeys objectAtIndex:indexPath.section];
//获取对应的分组
NSMutableArray *group = [_groupDic valueForKey:key];
//获取对应的人名
NSString *name = [group objectAtIndex:indexPath.row];

//给 cell 赋值
cell.textLabel.text = name;

// UITableView声明了一个NSIndexPath的类别,主要用来标识当前cell的在tableView中的位置,该类别有section和row两个属性,前者标识当前cell处于第几个section中,后者代 表在该section中的第几行。

return cell;
}

//设置侧边导航栏
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return _allKeys;
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end


<DetailViewController.h>

#import <UIKit/UIKit.h>

@interface DetailViewController : UIViewController

@property (nonatomic,retain)NSString *string;      //用来接受字符串

@end


<DetailViewController.m>

#import "DetailViewController.h"

@interface DetailViewController ()

@end

@implementation DetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

self.view.backgroundColor = [UIColor purpleColor];
//设置 title
self.navigationItem.title = _string;

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: