// // ViewController.m // 07-UITableView // // Created by kaiyi wang on 16/10/31. // Copyright © 2016年 Corwien. All rights reserved. // #import "ViewController.h" @interface ViewController ()<UITableViewDataSource,UITableViewDelegate> @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 1.添加tableView UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped]; // 设置行高 tableView.rowHeight = 50; // 底部放置控件 tableView.tableFooterView = [[UISwitch alloc] init]; // 设置数据源 tableView.dataSource = self; [self.view addSubview:tableView]; // self.tableView.dataSource = self; } #pragma mark - 数据源方法 /** * 一共多少组数据 */ -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 2; } /** * 第section组有多少行 */ -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if(section == 0) { return 3; }else{ return 4; } } /** * 每一行显示怎样的内容 */ -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 1.定义一个标示, static修饰局部变量,能够保证局部变量只分配一次存储空间(之初始化一次) static NSString *ID = @"myCell"; // 2.从缓存池中取出cell UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; // 3.若是缓存池中没有cell,则建立 if(cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; } // 设置Cell背景 // UIImageView *bgView = [[UIImageView alloc] init]; // bgView.image = [UIImage imageNamed:@"meizi.jpg"]; // cell.backgroundView = bgView; // cell.accessoryType = UITableViewCellAccessoryCheckmark; if(indexPath.section == 0){ // 第0组(影星) if(indexPath.row == 0) // 第0组的第0行 { cell.textLabel.text = @"Jack Ma"; } else if(indexPath.row == 1) { cell.textLabel.text = @"Jack Chan"; } else if(indexPath.row == 2) { cell.textLabel.text = @"Jet Lee"; } } else // 第1组(商人) { if(indexPath.row == 0) { cell.textLabel.text = @"Pony Ma"; } else if(indexPath.row == 1) { cell.textLabel.text = @"Judy Lee"; } else if(indexPath.row == 2) { cell.textLabel.text = @"Jobs Steve"; } else if(indexPath.row == 3) { cell.textLabel.text = @"Gates Bill"; } } return cell; } /** * 第section组显示怎样的头部xinxi */ -(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { if(section == 0){ return @"演员"; }else{ return @"商人"; } } /** * 第section组显示怎样的头部xinxi */ -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { if(section == 0){ return @"演员"; }else{ return @"商人"; } } /** * 选中行时 */ -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // NSLog(@"---select----%d", indexPath.row); // 0.取消选中这一行(去掉cell默认的蓝色背景) // [tableView deselectRowAtIndexPath:indexPath animated:YES]; // 1.取出选中行的Cell对象 UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath]; selectedCell.accessoryType = UITableViewCellAccessoryCheckmark; } @end