1 /** 2 UITableViewCellStyleDefault, 默认类型 标题+可选图像 3 UITableViewCellStyleValue1, 标题+明细+图像 4 UITableViewCellStyleValue2, 不显示图像,标题+明细 5 UITableViewCellStyleSubtitle 标题+明细+图像 6 */ 7 // 告诉表格每个单元格的明细信息 8 // 此方法的调用频率非常高! 9 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 10 { 11 NSLog(@"表格行明细 %d", indexPath.row); 12 13 // 0. 可重用标示符字符串 14 // static静态变量,能够保证系统为变量在内存中只分配一次内存空间 15 // 静态变量,一旦创建,就不会被释放,只有当应用程序被销毁时,才会释放! 16 static NSString *ID = @"Cell"; 17 18 // 1. 取缓存池查找可重用的单元格 19 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; 20 21 // 2. 如果没有找到 22 if (cell == nil) { 23 NSLog(@"实例化单元格"); 24 // 创建单元格,并设置cell有共性的属性 25 26 // 实例化新的单元格 27 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; 28 29 // 右侧箭头 30 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 31 32 // 背景颜色,会影响到未选中表格行的标签背景 33 // cell.backgroundColor = [UIColor redColor]; 34 // 在实际开发中,使用背景视图的情况比较多 35 // 背景视图,不需要指定大小,cell会根据自身的尺寸,自动填充调整背景视图的显示 36 // UIImage *bgImage = [UIImage imageNamed:@"img_01"]; 37 // cell.backgroundView = [[UIImageView alloc] initWithImage:bgImage]; 38 // UIView *bgView = [[UIView alloc] init]; 39 // bgView.backgroundColor = [UIColor yellowColor]; 40 // cell.backgroundView = bgView; 41 // 没有选中的背景颜色 42 // 选中的背景视图 43 // UIImage *selectedBGImage = [UIImage imageNamed:@"img_02"]; 44 // cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:selectedBGImage]; 45 } 46 47 // 设置单元格内容 48 HMHero *hero = self.heros[indexPath.row]; 49 50 // 标题 51 cell.textLabel.text = hero.name; 52 // 明细信息 53 cell.detailTextLabel.text = hero.intro; 54 // 图像 55 cell.imageView.image = [UIImage imageNamed:hero.icon]; 56 57 return cell; 58 }