iOS:uitableview将第一个单元格上的对象重绘到底部单元格

我有一个自定义的单元格与正常的代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; DDMainCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; if (cell == nil) { cell = [[DDMainCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } } 

问题是,当我select一个单元格,我在网上下载数据的单元格上添加进度栏..但是,当我向下滚动,我发现,每10个单元格有相同的进度条..我如何防止这种行为?

尝试这个,

  - (void)viewDidLoad { [super viewDidLoad]; dataarr=[[NSMutableArray alloc]init]; indexarr=[[NSMutableArray alloc]init]; mytableview=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain]; mytableview.dataSource=self; mytableview.delegate=self; [self.view addSubview:mytableview]; for (int i=0; i<30; i++) { [dataarr addObject:[NSString stringWithFormat:@"%d",i]]; } // Do any additional setup after loading the view, typically from a nib. } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return [dataarr count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ; } cell.textLabel.text=[dataarr objectAtIndex:indexPath.row]; UIActivityIndicatorView *act=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; [act setFrame:CGRectMake(50, 20, 20, 20)]; act.hidden=YES; [cell.contentView addSubview:act]; cell.selectionStyle=UITableViewCellSelectionStyleNone; if ([indexarr containsObject:[dataarr objectAtIndex:indexPath.row]]) { [act startAnimating]; act.hidden=NO; return cell; } return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if ([indexarr containsObject:[dataarr objectAtIndex:indexPath.row]]) { [mytableview reloadData]; return; } [indexarr addObject:[dataarr objectAtIndex:indexPath.row]]; [mytableview reloadData]; } 

确保下载完成后,从indexarr中删除这个对象….

那是因为你的细胞正在被重复使用; UITableView将把屏幕单元放到可重用的单元队列中,如果reuseIdentifier匹配,则将它们出列以便重用。 您应该使用一些其他数据结构(例如NSArrayNSDictionary )来跟踪哪些索引/单元格已被轻敲。 然后,在上面显示的方法中,无论该单元格是初始化的还是出列的,根据您的基础数据结构设置进度条。

这里你用过的UITableViewCellIdentifierreuseIdentifier 。 哪些将适用于所有单元格是相同的types。 现在你正在取一次细胞进度条。 现在它将不同于所有单元格数据。

因此,使用一个更多的表格单元格作为进度条,或者在重新加载表格时删除已经存在的进度条并重新添加。 为此使用进度条的标签。