iphone:uitableview:一个单元格的内容在滚动上发生变化

我有一个数组,我用来提供表格视图中的自定义单元格的内容我不知道什么是错的,但是当我滚动tableview的contants oc单元格dynamic变化

请帮我解决这个问题

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *MyIdentifier = @"MyIdentifier"; MyIdentifier = @"tblCellView"; NSString *offendersImagePath = [self applicationDocumentsDirectory]; //NSLog(@"%@", dbPath); offendersImagePath=[offendersImagePath stringByAppendingPathComponent:@"Images"]; CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier]; if(cell == nil) { [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; cell = aCustomCell; aCustomCell=nil; } NSMutableArray *tempArray;//=[[NSMutableDictionary alloc] init]; tempArray=[offendersNamesList objectAtIndex:indexPath.row]; //NSLog(@"%d",indexPath.row); offendersImagePath=[offendersImagePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg",[tempArray objectAtIndex:0]]]; NSLog(offendersImagePath); [[cell offendersImageView] setImage:[UIImage imageWithContentsOfFile:offendersImagePath]]; [cell.offendersNameLbl setText:[tempArray objectAtIndex:1]]; [cell.offendersViolation setText:[tempArray objectAtIndex:2]]; //[tempDictionary release]; //[cell setLabelText:[arryData objectAtIndex:indexPath.row]]; return cell; 

}

这些问题中通常会遇到的问题是,您没有在cellForRowAtIndexPath中正确设置内容。 当一个单元格滚动屏幕时,系统将其转换为“回收队列”(我的术语)。 当新的单元格滚动到屏幕上时,系统在这个循环队列中寻找它可以重用的单元。
((CustomCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];)
如果它没有find一个,它会从头开始build立一个全新的。 在你的情况下,无论出于什么原因,你都没有正确地设置单元格内容,而你所看到的变化是没有用正确内容更新的循环单元格。

我不确定你到底错在哪里,但你用于新单元的代码有点奇怪。 它应该看起来更像这样:

 if(cell == nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; cell = [nib objectAtIndex:0]; } 

我不是100%你的问题是什么,但如果它是可变文本长度的项目和单元格中的项目没有得到重新大小,这是因为包含在单元格中的标签的大小没有得到更新,只是他们的文字正在改变。

有关在cellForRowAtIndexPath中执行查找的注意事项,也许您应该在viewDidLoad中构造您的数组(对象/字典),以便更高效。

另外要注意的是你使用.jpg文件。 使用.png文件更好,因为它们在编译时被压缩了。

还使用NSMutableArray * tempArray; 和硬编码索引是非常糟糕的做法,如果在数组中改变位置,这意味着你必须改变你的所有代码。 尝试使用NSDictionary,以便键不太可能改变。