定义了一个UITableViewCell,
官方文档定义一个cell的时候是这样写的
static NSString *identify= @"mycell;
}
但是在每个cell上面添加一些控件(例如 UILabel UItextField 的对象 add在cell。contentView上),就会出现滑动时重画出错混乱的问题
而直接使用cell.textLabel.text显示是没有这个问题,
解决方法
google了一下,目前已有的解决方案是将
1
- cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
替换成
- cell = [tableView cellForRowAtIndexPath:indexPath];
2
- cell = nil;
这们做的目的去掉Cell的重用机制,但是这种方法都会在后台随着表格滚动一直在创建cell,
3
还有一种解决方案是自己定义Cell数组,在tableView:tableView cellForRowAtIndexPath:中进设置要显示的cell,这是手工维护cell的一种方式,对大数据量的情况肯定是不适用的,不过也能算得上是一种思路吧,可以参考一下。其代码如下:
- //在构造函数里定义cell数组
- for(int i = 0; i < 31; i ++)
- {
- static NSString *MyBookMarkIdentifier = @"CityMangerCell";
- cityCell[i] = [[CityMangerCell alloc] initWithFrame.:CGRectZero reuseIdentifier:MyBookMarkIdentifier initIndex:i];
- }
- //使用它
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- if((0 <= indexPath.row) && (indexPath.row < 31))
- return cityCell[indexPath.row];
- return nil;
- }
4
还有一种方法 是不用重画cell。可以重画cell上的控件,
正确的做法应该是在if (cell == nil){}判断后面进行赋值。即
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefa ult
reuseIdentifier: SimpleTableIdentifier] autorelease]; }
- 删除之前的控件 重新设置控件。但是也是比较繁琐
希望对大家有所帮助