创建 tableView
UITableViewStyle 有两种选择
#pragma mark - 创建 tableView- (void)createTableView{ // 枚举类型共同拥有两个 self.mainTableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain]; [self addSubview:self.mainTableView];}
根视图控制器遵守协议
@interface RootViewController ()
- (void)viewDidLoad { [super viewDidLoad]; // 设置数据源代理 self.rootView.mainTableView.dataSource = self; // 设置 delegate self.rootView.mainTableView.delegate = self; self.title = @"联系人";}
重写代理方法
返回分组的个数
#pragma -mark 返回分组的个数// 默觉得 1- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 2;}
每一个分组显示多少行数据 必须
#pragma mark - 每一个分组显示多少行数据 *必须*// dataSource 下方法- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.nameArray.count;}
每行显示什么内容 必须
#pragma mark - 每行显示什么内容 *必须*// dataSource 下方法- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ // 四种类型 // UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil]; // static 作用 /** * 1、清零功能 static int a; * 2、保值作用 * 3、隐藏功能 */ // 静态变量 保值作用 仅仅创建一次 static NSString *cell_id = @"UITableViewCell"; // 利用重用创建 UITableViewCell *cell = nil; // 在重用池查找 cell = [tableView dequeueReusableCellWithIdentifier:cell_id]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cell_id]; } cell.textLabel.text = self.nameArray[indexPath.row];// indexPath.section 那个分组// indexPath.row 哪一行 NSString *name = [NSString stringWithFormat:@"%ld.png", indexPath.row + 1]; cell.imageView.image = [UIImage imageNamed:name]; // 显示具体信息 cell 类型用 UITableViewCellStyleSubtitle cell.detailTextLabel.text = self.numberArray[indexPath.row]; // 右側附件button 枚举类型 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; return cell;}
row 的高度
#pragma mark - 跳转 row 的高度- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 80.0f;}
返回分组的名字
#pragma mark - 返回分组的名字- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ if (section == 0) { return @"分组一"; } return @"分组二";}
设置分组头部
#pragma mark - 设置分组头部- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 50)]; label.textAlignment = NSTextAlignmentCenter; label.textColor = [UIColor redColor]; if (section == 0) { label.text = @"1组"; } else { label.text = @"2组"; } return label;}
设置分组头部的高度
#pragma mark - 设置分组头部的高度- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 60.0f;}#pragma mark - 设置分组头部的高度- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 60.0f;}
cell 的点击事件
#pragma mark - cell 的点击事件- (void)tableView:(UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ // 取消选中状态 [tableView deselectRowAtIndexPath:indexPath animated:YES]; SecondViewController *secondVC = [[SecondViewController alloc] init]; [self.navigationController pushViewController:secondVC animated:YES];}
实现索引
#pragma mark - 实现索引- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{ return @[@"1组", @"2组"];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}