//
// RootViewController.m
#import "RootViewController.h"
#import "SecondViewController.h"
@interfaceRootViewController()<UITableViewDataSource,UITableViewDelegate,SecondViewControllerDelegate>
@property (nonatomic,strong)NSArray *arr;
@property(nonatomic,strong)NSMutableDictionary *dic;
@property (nonatomic,strong)UITableView*tableView;
@end
@implementationRootViewController
- (void)viewDidLoad {
[superviewDidLoad];
self.title = @"通讯录";
self.view.backgroundColor =[UIColoryellowColor];
UITableView *tableView =[[UITableViewalloc]initWithFrame:[UIScreenmainScreen].boundsstyle:UITableViewStylePlain];
self.tableView = tableView;
tableView.backgroundColor =[UIColorredColor];
// 设置分割线颜色
tableView.separatorColor =[UIColorgreenColor];
// 设置分割线样式
// tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//设置行高
tableView.rowHeight = 100;
// 设置表视图的代理
// datasource主要是提供数据,数据源的代理。
// delegate 主要是外观之类的设置
tableView.dataSource = self;
tableView.delegate = self;
[self.viewaddSubview:tableView];
NSMutableArray *arr1 = [NSMutableArrayarrayWithObjects:@"张尼玛",@"土耳其",@"普京",nil];
NSMutableArray *arr2 =[NSMutableArrayarrayWithObjects:@"赵日天",@"叶良辰",@"吴栋生",@"挑fen男孩",@"隔壁老王",nil];
// self.arr = @[arr1,arr2];
NSMutableDictionary *dictionary =[NSMutableDictionary dictionaryWithObjectsAndKeys:arr1,@"A",arr2,@"B",nil];
self.dic = dictionary;
self.arr =@[@"A",@"B"];
}
// 设置分区的行数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
returnself.arr.count;
// return 2;
}
// 数据源的协议方法(有多少个分区就进入这个方法多少次,每次获取一个分区下标.)
- (NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section{
// if(section == 0){
// // 根据对应分的数组中的元素个数决定单元格个数
// //return [self.arr[section]count];
//// return 3;
//
// }else{
// // return [self.arr[section] count];
//// return 5;
// }
// 设置每个分区的单元格个数+
// self.arr[section] 根据分区获得key值,self.dic[]根据key值获得数组
return [self.dic[self.arr[section]]count];
}
//配置单元格数据
- (UITableViewCell *)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath{
// 定义一个重用标识符
staticNSString *reuse = @"log";
// 从重用队列中取出cell
UITableViewCell *cell =[tableViewdequeueReusableCellWithIdentifier:reuse];
// 重用队列中存放的格式一样的cell,内容不一样,创建的单元格总共为屏幕可容纳的cell个数,前面的内容不在屏幕内后,cell就进入重用队列中,这时重用队列中的cell用来显示后面进入屏幕的内容。
// 从重用队列中取出可用(重用标识符一样)的cell
// 如果cell为空,(重用队列中无此cell)
if (cell == nil) {
// 新建一个cell
cell =[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:reuse];
NSLog(@"没有此单元格,新建一个单元格");
}else{
NSLog(@"这时重用的单元格");
}
cell.imageView.image =[UIImageimageNamed:@"h2.jpeg"];
// NSIndexPath有两个属性:
// 通过indexpath.row访问单元格
// 通过indexPath.section访问分区
// 随着屏幕的滚动indexPath会变化
//用indexPath.section获取到当前分区,indexPath.row获取到当前单元格。
// cell.textLabel.text = self.arr[indexPath.section][indexPath.row];
// cell.textLabel.text = self.arr[indexPath.row];
// // 首先拿到对应分区的key值
// NSString *key = self.arr[indexPath.section];
// // 根据key值获取对应分区的数组
// NSArray *arr = [self.dicvalueForKey:key];
// // 通过数组为单元格赋值
// cell.textLabel.text = arr[indexPath.row];
cell.textLabel.text =self.dic[self.arr[indexPath.section]][indexPath.row];
// cell.detailTextLabel.text = @"