0

我有一个带有自定义单元格的 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];



    }
}

问题是当我选择一个单元格时,我在在线下载数据的单元格上添加了进度条.. 但是当我向下滚动时,我发现每 10 个单元格都有相同的进度条.. 我怎样才能防止这种行为?

4

3 回答 3

1

试试这个,

 - (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 中删除此对象...。

于 2013-02-19T10:04:28.973 回答
0

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

于 2013-02-19T09:56:29.347 回答
-1

这里你使用UITableViewCellIdentifier的是reuseIdentifier. 这适用于所有相同类型的单元格。现在您正在使用带有进度条的单元格。现在它将与所有单元格数据不同。

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

于 2013-02-19T12:49:27.580 回答