A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© yuanlingqi 中级黑马   /  2014-12-2 23:07  /  1024 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 yuanlingqi 于 2014-12-2 23:18 编辑

UITableView的使用完全依赖于代理模式的实现。视频中,李老师选择了较为复杂和浪费内存空间的方式来实现了打钩处理,
具体就是使用了一个数组来存放选中元素,然后在加载cell时候,判断元素是否被已选对象数组包含,来进行打钩处理。
猜测李老师之所以选择这种较纠结实现方式来讲解,是为了更好的锻炼学员的思路,的确用心良苦。
较好的打钩实现方式当然是增加对象属性了,根据属性来决定是否已打钩,本例子就使用了增加属性的打钩方式,
并实现了点一次打钩,再点则去除打钩的逻辑,不多说,上代码。
  1. //
  2. //  CJPViewController.m
  3. //  tableView性能优化
  4. //
  5. //  Created by lucky on 14/12/1.
  6. //  Copyright (c) 2014年 cn.itcast. All rights reserved.
  7. //

  8. #import "CJPViewController.h"
  9. #import "Shop.h"
  10. @interface CJPViewController ()<UITableViewDataSource,UITableViewDelegate>{
  11.     NSMutableArray *shopArray;
  12. }
  13. @end

  14. @implementation CJPViewController

  15. - (void)viewDidLoad
  16. {
  17.     [super viewDidLoad];
  18.     //获取字典文件路径
  19.     NSString *path = [[NSBundle mainBundle]pathForResource:@"shops.plist" ofType:nil];
  20.     //读取文件
  21.     NSArray *array = [NSArray arrayWithContentsOfFile:path];
  22.     //实例化对象数组
  23.     shopArray = [NSMutableArray array];
  24.     //声明临时变量
  25.     Shop *shop;
  26.     //将字典对象模型化
  27.     for (NSDictionary *dic in array) {
  28.         shop = [Shop shopWithDictionary:dic];
  29.         [shopArray addObject:shop];
  30.     }
  31. }

  32. #pragma mark 指定section的行数
  33. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  34.     return shopArray.count;
  35. }

  36. #pragma 加载每行数据
  37. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  38.     //1.定义标识
  39.     static NSString *id = @"mycell";
  40.     //2.从缓存池中获取cell
  41.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:id];
  42.     //3.若为空则创建实例
  43.     if (cell == nil) {
  44.         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:id];
  45.     }
  46.    
  47.     //4.显示商品
  48.     Shop *s = shopArray[indexPath.row];
  49.     //设置名称
  50.     cell.textLabel.text = s.name;
  51.     //设置描述
  52.     cell.detailTextLabel.text = s.desc;
  53.     //设置图片
  54.     cell.imageView.image = [UIImage imageNamed:s.icon];
  55.    
  56.     //若曾被选中,则打钩,否则去除标记
  57.     if (s.gou == YES) {
  58.         cell.accessoryType = UITableViewCellAccessoryCheckmark;
  59.     }else{
  60.         cell.accessoryType = UITableViewCellAccessoryNone;
  61.     }
  62.     return cell;
  63. }

  64. #pragma mark 设定行高
  65. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  66.     return 70;
  67. }

  68. #pragma mark 选中方法
  69. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  70.     NSLog(@"-----%d",indexPath.row);
  71.     //去除选中状态
  72.     [tableView deselectRowAtIndexPath:indexPath animated:YES];
  73.     //设置打钩的值
  74.     Shop *s = shopArray[indexPath.row];
  75.     //若已经打钩则去除打钩,若未打钩则打钩
  76.     s.gou = s.gou ==YES?NO:YES;
  77.     //替换数据源中的对象
  78.     [shopArray replaceObjectAtIndex:indexPath.row withObject:s];
  79.     //刷新数据
  80.     [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationAutomatic)];
  81. }
  82. @end
复制代码






0 个回复

您需要登录后才可以回帖 登录 | 加入黑马