滚动时在UITableView中重复数据

我使用波纹pipe代码来显示TableView上的数据,但滚动时,数据重复和其他数据丢失。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [FileCompletedArray count]; } 

cellForRowatindexpath():

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CellIdentifier"; UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; UILabel *FileNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)]; FileNameLabel.backgroundColor = [UIColor clearColor]; FileNameLabel.font = [UIFont fontWithName:@"Helvetica" size:16]; FileNameLabel.font = [UIFont boldSystemFontOfSize:16]; FileNameLabel.textColor = [UIColor blackColor]; NSLog(@"Reseversed TEMP array %@",FileCompletedArray); FileNameLabel.text =[FileCompletedArray objectAtIndex:indexPath.row]; [cell.contentView addSubview: FileNameLabel]; [FileNameLabel release]; } return cell; } 

你有什么解决办法? 提前致谢

使用单元格标识符不同

比如像波纹pipe

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *CellIdentifier = [NSString stringWithFormat:@"%d,%d",indexPath.section,indexPath.row]; UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; /// write your code here.. } } 

或者像下面那样设置nil ..

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //static NSString *CellIdentifier = @"CellIdentifier"; UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:nil]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease]; /// write your code here.. } } 

这个问题是由于你的UITableView重用单元的瞬间产生的。 我给你一些可能对你有帮助的build议。

1)在两者之间添加Controller

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { /// Your code of initialization of controller; } /// set property of controllers such like (Text UILabel, image of UIImageView...etc) . return cell; } 

1)将你的Controller添加到cell.contentView.

编辑:

在你遵循我编辑的答案之前,我想告诉你,下面的代码对于内存pipe理是不好的,因为它会为每一行UITableView创build新的单元格,所以要小心。

但是,如果UITableView有限的行(50-100可能是)使用下面的代码,如果它适合你更好。

 NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row]; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if(cell == nil) { /// Your whole code of controllers; } 

只有在创build新单元格时,才会设置标签的文本。 但是,在小区重用期间,不会创build新的小区。 因此,您设置/更改文本的代码不起作用。 你可以使用这个修改后的代码。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CellIdentifier"; UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier]; UILabel *FileNameLabel=nil; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; FileNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)]; FileNameLabel.tag = 1001; FileNameLabel.backgroundColor = [UIColor clearColor]; FileNameLabel.font = [UIFont fontWithName:@"Helvetica" size:16]; FileNameLabel.font = [UIFont boldSystemFontOfSize:16]; FileNameLabel.textColor = [UIColor blackColor]; [cell.contentView addSubview: FileNameLabel]; [FileNameLabel release]; } if(!FileNameLabel) FileNameLabel = [cell.contentView viewWithTag:1001]; FileNameLabel.text =[FileCompletedArray objectAtIndex:indexPath.row]; return cell; } 

或者,您可以使用默认的textLabel而不是创build和添加新的标签

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CellIdentifier"; UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier]; UILabel *FileNameLabel=nil; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:16]; cell.textLabel.font = [UIFont boldSystemFontOfSize:16]; cell.textLabel.textColor = [UIColor blackColor]; } cell.textLabel.text =[FileCompletedArray objectAtIndex:indexPath.row]; return cell; } 

我也面临同样的问题,当我们重复使用单元格时会出现问题。 在重用的时候,数据已经存在了,我们也写了一些更多的数据,为了解决这个问题,我对表视图的委托方法cellForRowAtIndexPath做了一些小改动。 这里的代码,我总是使用表视图。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; if (!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"]; } for (UIView *view in cell.contentView.subviews) { [view removeFromSuperview]; } return cell; 

}

当我们重新使用某个单元格时,for循环被执行,它所做的是删除UITableViewCell中的所有以前的数据

希望这会帮助别人。