解析UITableViewCell的reuse问题
来源:互联网 发布:java的api文档在哪里 编辑:程序博客网 时间:2024/05/23 02:03
http://blog.csdn.net/xunyn/article/details/8316358
UITableView在iOS开发中会经常使用,对于行数很多的UITableView,可以通过UITableViewCell的重用,来保证执行效率。源码下载点击这里。
我们通过代码来探索UITableViewCell重用的实现,下面是一段使用UITableView的代码,
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *CellIdentifier = @"myCell";
- //NSString *CellIdentifier = [NSString stringWithFormat:@"myCell_%d",indexPath.row];
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (cell == nil) {
- cell = [[UITableViewCell alloc] initWithStyle:UITableViewStyleGrouped reuseIdentifier:@"myCell"];
- UITextField *tf;
- tf = [[UITextField alloc] initWithFrame:CGRectMake(100, 10, 150, 40) ];
- tf.delegate = self;
- tf.borderStyle = UITextBorderStyleRoundedRect;
- [cell addSubview:tf];
- [tf release];
- }
- // Configure the cell...
- cell.textLabel.text = [NSString stringWithFormat:@"%d",indexPath.row];
- return cell;
- }
运行结果是这样
我们在textfield里输入label的序号,然而我们上下拖动后,结果是textfield的值并没有得到保存,其随着cell的重用而变化。
我们回到dequeueReusableCellWithIdentifier的定义
- - (id)dequeueReusableCellWithIdentifier:(NSString *)identifier; // Used by the delegate to acquire an already allocated cell, in lieu of allocating a new one.
reuseIdentifier(“myCell”)在对象池中,查找之前已经放入的cell对象。
然后从对象池中,取出之前放入的,然后执行
- // Configure the cell...
- cell.textLabel.text = [NSString stringWithFormat:@"%d",indexPath.row];
所以我们需要为textfield里的text内容设置model层,然后配置textfield的内容,像我们对textLabel的设置一样
还有了不完美的解决方案,既然它重用出问题,就不让它重用,代码如下
- NSString *CellIdentifier = [NSString stringWithFormat:@"myCell_%d",indexPath.row];
- 解析UITableViewCell的reuse问题
- 解析UITableViewCell的reuse问题
- 关于端口reuse的问题
- 解析UITableViewCell的重用
- UITableViewCell的重用问题
- UITableViewCell的自定义问题
- UITableViewCell的重用问题
- [转]resin的session id reuse特性(1)--发现问题
- uitableviewcell无限添加的问题
- uitableviewcell的生成crash问题
- storyboard 拖 UITableviewCell的问题
- 设置UITableViewCell高度的问题
- iOS UITableViewCell的性能问题
- UITableViewCell的选择状态问题
- 使用UITableViewCell遇到的问题
- 自定义UITableViewCell的旋转问题
- 上边的UITableViewCell覆盖下边的UITableViewCell的问题
- UITableViewCell 的backgroundColor不起作用的问题?
- Java 线程池的原理与实现
- Android对Linux内核的增强
- 新发现框架
- 《企业软件交付:敏捷与高效管理精要》迷你书
- 识别码知识-UUID
- 解析UITableViewCell的reuse问题
- 经典小故事
- NS2上如何在有线网络中组播H.264/SVC视频
- 高阶Perl
- SQLSERVER行列的动态转换
- JSON数据格式
- 前端性能优化:DocumentFragments或innerHTML取代复杂的元素注入
- SESSION 有效期
- Google 的工程师文化