我在写一个App的时候自定义了一个UITableViewCell,可是这个UITableView在运行的时候出现了每6行数据就循环重复显示的问题,而直接使用cell.textLabel.text显示是没有这个问题,如下是我实现的代码。数组
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- NSInteger section = [indexPath section];
- NSInteger row = [indexPath row];
- UITableViewCell *cell;
- switch (section)
- {
- case 0:
- //do something.
- case 1:
- cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
- if (cell == nil)
- {
- cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"] autorelease];
- //Image
- UIImageView *p_w_picpath = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 14.0f, 45.0f, 50.0f)];
- p_w_picpath.backgroundColor = [UIColor clearColor];
- p_w_picpath.p_w_picpath = [UIImage p_w_picpathNamed:@"folder.png"];
- [cell.contentView addSubview:p_w_picpath];
- [p_w_picpath release];
- //Label
- UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(45.0f, 6.0f, 214.0f, 50.0f)];
- titleLabel.text = (NSString *)[(NSArray *)[self.categoryArray objectAtIndex:1] objectAtIndex:row];
- NSLog(@"%@ -- %d", titleLabel.text, row);
- titleLabel.textAlignment = UITextAlignmentLeft;
- titleLabel.numberOfLines = 3;
- titleLabel.tag = 201;
- titleLabel.font = [UIFont boldSystemFontOfSize:14];
- [cell.contentView addSubview:titleLabel];
- [titleLabel release];
- }
- cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
- break;
- }
- cell.selectionStyle = UITableViewCellSelectionStyleNone;
- return cell;
- }
google了一下,目前已有的解决方案是将ide
- cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
替换成函数
- cell = [tableView cellForRowAtIndexPath:indexPath];
或大数据
- cell = nil;
这们作的目的去掉Cell的重用机制,可是这种方法都会在后台随着表格滚动一直在建立cell,经过上面源代码中Label定义里那句NSLog在控制台输出就能够看到,虽然会自动回收内存,但确定也会给系统带来不小开销,因此不到万一得以仍是不会用的。google
还有一种解决方案是本身定义Cell数组,在tableView:tableView cellForRowAtIndexPath:中进设置要显示的cell,这是手工维护cell的一种方式,对大数据量的状况确定是不适用的,不过也能算得上是一种思路吧,能够参考一下。其代码以下:spa
- //在构造函数里定义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;
- }
后来我仔细分析了一下程序,找到了问题所在:xml
缘由是在if (cell == nil)判断内部不该该对其label进行赋值,即不使用这句:内存
- titleLabel.text = (NSString *)[(NSArray *)[self.categoryArray objectAtIndex:1] objectAtIndex:row];
正确的作法应该是在if (cell == nil){}判断后面进行赋值。即ci
- if (cell == nil)
- {
- ....
- }
- UILabel *l1 = (UILabel *)[cell.contentView viewWithTag: 201];
- l1.text = (NSString *)[(NSArray *)[self.categoryArray objectAtIndex:1] objectAtIndex:row];
分析缘由以下:
UITableView中被实例化的cell个数由屏高和每一个cell的高度决定,由于个人cell高度设置为80,一屏只能 显示6个Cell(只有6个cell被实例化),也就是只有这6个cell才会执行if (cell == nil){}中的代码,从第6行日后的cell都是重用的这6个cell,也就是说从第7行开始将不会执行if (cell = nil){}中的代码,当UITableView须要绘制第7行cell的时候,会取得第1个cell进行重用,若是咱们不把原来第1行cell中的 Label内容进行修改,那么第7行将彻底显示第1行中的内容,因此才会在第6行以后开始出现数据重复的状况。
如今我将Label内容设置的代码放到if (cell == nil){}以后,它将会对每个被重用的cell的Label进行设定,也就不会再出现cell内容重复的现象。
但愿这个问题的解决过程会对你们有所帮助。string