UItableviewCell重用注意事项


注意事项:

1,在cellForRowAtIndexpath里如果未对cell中的控件赋值,由于重用的原因,会出现界面错乱

2, 解决方案:model里存储的控件要显示的内容每次cell重用,根据model重新赋值,

3,当cell上的控件发生变化时,及时修改model

重用cell经常会出现重用错误的现象,下面就通过代码的形式帮助解决一下这个问题

原理就是通过将cell中的值放在一个数组中,通过数组的方式不断控制cell(下面通过两种不同的方式来解决这个问题)

第一步创建一个A对象

  1. @interface A : NSObject  
  2.   
  3. @property (nonatomic, assign)BOOL on;  
  4.   
  5. @end  

里面只有一个属性。控制开关的开与关

第二步创建一个自定义cell

  1. @interface CustomCell : UITableViewCell  
  2.   
  3. // 开关  
  4. @property (nonatomicretainUISwitch *s;  
  5. // 索引  
  6. @property (nonatomicretainNSIndexPath *indexPath;  
  7.   
  8. @end  
实现代码,在cell上添加一个开关
  1. @implementation CustomCell  
  2.   
  3. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier  
  4. {  
  5.     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];  
  6.     if (self) {  
  7.         _s = [[UISwitch alloc] initWithFrame:CGRectMake(200106020)];  
  8.         [self.contentView addSubview:_s];  
  9.         
  10.         // Initialization code  
  11.     }  
  12.     return self;  
  13. }  
  14. - (void)setSelected:(BOOL)selected animated:(BOOL)animated  
  15. {  
  16.     [super setSelected:selected animated:animated];  
  17.   
  18.     // Configure the view for the selected state  
  19. }  
  20. @end  
第三步定义一个UITableViewController对象
  1. @interface TableViewController : UITableViewController<CustomCellDelegate>  
  2.   
  3. @property (nonatomicretain)NSMutableArray *arr;  
  4.   
  5. @end  
只有一个用来保存信息的数组

实现

  1. - (id)initWithStyle:(UITableViewStyle)style  
  2. {  
  3.     self = [super initWithStyle:style];  
  4.     if (self) {  
  5.         // 默认开关的状态为关,把开关状态信息添加到数组中  
  6.         self.arr = [NSMutableArray arrayWithCapacity:30];  
  7.   
  8.         for (int i = 0 ; i < 30; i++) {  
  9.             A *a = [[A alloc] init];  
  10.             a.on = NO;  
  11.             [self.arr addObject:a];  
  12.         }  
  13.         // Custom initialization  
  14.     }  
  15.     return self;  
  16. }  
  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     static NSString *CellIdentifier = @"Cell";  
  4.     CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
  5.     if (cell == nil) {  
  6.         cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];  
  7.        [cell.s addTarget:self action:@selector(changeState:) forControlEvents:UIControlEventValueChanged];  
  8.     }  
  9.     // indexPath中存储的是所在的行和列  
  10.     cell.indexPath = indexPath;  
  11.     A *a = [self.arr objectAtIndex:indexPath.row];  
  12.     // 通过改变值得方式改变cell的内容(只要数组中值不变,就不会发生)  
  13.     cell.s.on = a.on;  
  14.      
  15.     return cell;  
  16. }  
  1. - (void)changeState:(UISwitch *)s  
  2. {  
  3.     // 通过获取的cell(中保存的indexPath来获取数组中对应的元素)   
  4.     CustomCell *cell = (CustomCell *)(s.superview.superview.superview);  
  5.     A *a = [self.arr objectAtIndex:cell.indexPath.row];  
  6.     a.on = s.on;  
  7. }  
以上方法是方法一


方法二通过代理的方式来实现重用机制

在自定义cell中写一个协议

  1. @protocol CustomCellDelegate <NSObject>  
  2. // 开关事件(参数为开关对象和索引对象)  
  3. - (void)switchDidClick:(UISwitch *)s atIndexPath:(NSIndexPath *)indexPath;  
  4.   
  5. @end  
  6.   
  7. @interface CustomCell : UITableViewCell  
  8.   
  9. // 开关  
  10. @property (nonatomicretainUISwitch *s;  
  11. // 索引  
  12. @property (nonatomicretainNSIndexPath *indexPath;  
  13. // 代理对象  
  14. @property (nonatomic, assign) id<CustomCellDelegate>delegate;  

方法体中
  1. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier  
  2. {  
  3.     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];  
  4.     if (self) {  
  5.         _s = [[UISwitch alloc] initWithFrame:CGRectMake(200106020)];  
  6.         [self.contentView addSubview:_s];  
  7.         [_s addTarget:self action:@selector(changeState:) forControlEvents:UIControlEventValueChanged];  
  8.         // Initialization code  
  9.     }  
  10.     return self;  
  11. }  
  12.   
  13. - (void)changeState:(UISwitch *)s  
  14. {  
  15.     // 返回调用cell所在indexPath中的内容(当前cell所在的indexPath)  
  16.     [_delegate switchDidClick:s atIndexPath:_indexPath];  
  17. }  
通过传递开关对象和所在的索引地址来确定所需要获取的数组的对象
  1. @interface TableViewController : UITableViewController<CustomCellDelegate>  
  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     static NSString *CellIdentifier = @"Cell";  
  4.     CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
  5.     if (cell == nil) {  
  6.         cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];  
  7.         cell.delegate = self;  
  8.     }  
  9.     // 创建后进行复制操作(索引)  
  10.     cell.indexPath = indexPath;  
  11.     A *a = [self.arr objectAtIndex:indexPath.row];  
  12.     cell.s.on = a.on;  
  13.     return cell;  
  14. }  
  15.   
  16. - (void)switchDidClick:(UISwitch *)s atIndexPath:(NSIndexPath *)indexPath  
  17. {  
  18.     A *a = [self.arr objectAtIndex:indexPath.row];  
  19.     // 获取A对象的具体位置,通过switch的具体操作值来进行数组中元素的修改  
  20.     a.on = s.on;  
  21. }  
以上方式就可以解决cell重复被使用的问题

转载自:http://blog.csdn.net/flytomyskyone/article/details/24651567
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值