Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS:uitableview从第一个单元格到底部单元格重绘对象_Ios_Uitableview_Scroll - Fatal编程技术网

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

iOS:uitableview从第一个单元格到底部单元格重绘对象,ios,uitableview,scroll,Ios,Uitableview,Scroll,我有一个自定义单元格的uitableview。。正常代码 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; DDMainCell *cell = [tableView dequeueReusableCellWithIdentif

我有一个自定义单元格的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个单元格都有相同的进度条。。如何防止这种行为

那是因为你的细胞被重新利用了
UITableView
将屏幕单元格推迟到可重用单元格队列中,如果
reuseIdentifier
匹配,则将其出列以供重用。您应该使用一些其他数据结构(例如,
NSArray
NSDictionary
)来跟踪已点击的索引/单元格。然后,在上面显示的方法中,无论单元格是初始化的还是出列的,都要根据您的底层数据结构设置进度条。

试试这个

 - (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];

}
-(void)viewDidLoad
{
[超级视图下载];
dataarr=[[NSMutableArray alloc]init];
indexarr=[[NSMutableArray alloc]init];
mytableview=[[UITableView alloc]initWithFrame:self.view.bounds样式:UITableViewStylePlain];
mytableview.dataSource=self;
mytableview.delegate=self;
[self.view addSubview:mytableview];

对于(int i=0;i这里,您使用的
UITableViewCellIdentifier
reuseIdentifier
。它将适用于所有类型相同的单元格。现在,您使用进度条获取一次单元格。现在,它将不同于所有单元格数据


因此,请为进度条使用更多的tableview单元格,或者在重新加载表格时,删除已经存在的进度条并重新添加。对于进度条的此使用标记。

我这样做了,但它仍然发生在较低的单元格中。您可以通过放置断点来检查。向下滚动时的索引是什么..插入时与之前进行比较在arr.i中,如果([ToDoindesPath containsObject:indexPath]){[cell.contentView addSubview:_redprogressBar];}或者{u redprogressBar.hidden=TRUE;}则将此代码放入单元格中的行中但现在它消失了,当它消失的时候再也不会回来了?当你点击单元格时。它可见吗?当我开始从两个单元格滚动时它消失了,当我点击单元格时它出现了