----Make by -LJW 转载请注明出处--- 缓存
1 #pragma make 数据源方法 2 //一共有多少组数据 3 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 4 { 5 return 1; 6 } 7 //第section组有多少row(一组有多少行) 8 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 9 { 10 return self.heros.count; 11 } 12 13 #pragma make TableView性能优化 14 //****************************************************************************** 15 16 // cellForRowAtIndexPath 显示的内容(显示的主要内容) 17 /** 18 * 每当有一个cell进入视野范围内,就会调用 19 */ 20 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 21 { 22 /* 23 //显示名字和详情的方法 24 //(用户每次拖拽页面都会从新建立新的TableView,多以会有性能问题) 25 //为了解决这个问题,会把多出的一个TableView放到缓存区,用户拖拽的时候用到在拿出来,避免建立多余的,TableView循环使用 26 UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil]; 27 */ 28 // static修饰局部变量:能够保 证局部变量只分配一次存储空间(只初始化一次) 29 static NSString *ID = @"hero"; 30 // 1.经过一个标识去缓存池中寻找可循环利用的cell 31 // dequeue : 出列 (查找) 32 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; 33 34 // 2.若是没有可循环利用的cell 35 if (cell == nil){ 36 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; 37 // NSLog(@"------缓存池找不到cell--%d", indexPath.row); 38 } 39 // 3.给cell设置新的数据 40 //取出模型 41 JWhero *hero = self.heros[indexPath.row]; 42 //设置名字 43 cell.textLabel.text = hero.name; 44 //添加第二列详情 45 cell.detailTextLabel.text = hero.intro; 46 //设置头像 47 cell.imageView.image = [UIImage imageNamed:hero.icon]; 48 49 //添加cell右边指示器的样式 50 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 51 //添加cell右边指示器按钮 52 // cell.accessoryView = [[UISwitch alloc]init]; 53 54 //设置cell默认背景图片 或者 背景颜色(背景view不用设置尺寸,优先级backgroundView > backgroundColor),因此设置默认背景颜色最好用backgroundView 55 // UIView *bgView = [[UIView alloc]init]; 56 UIImageView *bgView = [[UIImageView alloc] init]; 57 bgView.image = [UIImage imageNamed:@"buttongreen"]; 58 // bgView.backgroundColor = [UIColor redColor]; 59 cell.backgroundView = bgView; 60 61 //设置cell点击状态背景颜色 62 UIView *selectedbgView = [[UIView alloc]init]; 63 selectedbgView.backgroundColor = [UIColor yellowColor]; 64 cell.selectedBackgroundView = selectedbgView; 65 66 return cell; 67 }