Ios 自定义单元格图像将每3个单元格重复一次

Ios 自定义单元格图像将每3个单元格重复一次,ios,uitableview,Ios,Uitableview,我在表格的自定义单元格中添加一些图像时遇到一些问题。在每个单元格中创建一个ui视图,然后为其分配一个唯一的标记。我一直在尝试将图像添加到一个特定的单元格中进行测试,例如使用标记“2204”,但它仍然会将该图像添加到每三个单元格中(2201、2204等等),因此我不确定是什么原因导致了这种情况。我在每个单元格中设置了一个标签来显示视图的当前标记,它显示标记是正确的,但为什么它会将图像放置在其他单元格中 我在这个表中只有一个部分,它只是按行进行。默认情况下,显示5行,但可以添加更多行 - (UITa

我在表格的自定义单元格中添加一些图像时遇到一些问题。在每个单元格中创建一个
ui视图
,然后为其分配一个唯一的标记。我一直在尝试将图像添加到一个特定的单元格中进行测试,例如使用标记“2204”,但它仍然会将该图像添加到每三个单元格中(2201、2204等等),因此我不确定是什么原因导致了这种情况。我在每个单元格中设置了一个标签来显示视图的当前标记,它显示标记是正确的,但为什么它会将图像放置在其他单元格中

我在这个表中只有一个部分,它只是按行进行。默认情况下,显示5行,但可以添加更多行

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    workoutTableViewCell *cell = (workoutTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[workoutTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.exerciseViewArea.tag = 2200 + indexPath.row;

    //using fifth cell as a test
    testview = [self.view viewWithTag:2204];
    return cell;
}

- (void)changeExerciseImage
{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(9,10,20,20)];
    imageView.image = [UIImage imageNamed:@"testImage.png"];
    [testview addSubview:imageView];
    [testview bringSubviewToFront:imageView];

    NSLog(@"changed exercise image to %@", _exerciseTempText);
}

单元格可以被
UITableView
重用,因此与其保留对单个单元格的引用,不如更新数据源,然后调用
reloadData
reloadditemsatindexpaths
。例如,您可以为每个单元格使用图像名称的
NSMutableArray
,然后执行如下操作:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    workoutTableViewCell *cell = (workoutTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[workoutTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(9,10,20,20)];
    [imageView setTag:myImageViewTag];
    [cell.exerciseViewArea addSubview:imageView];
    [cell.exerciseViewArea bringSubviewToFront:imageView];

    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIImageView *imageView = (UIImageView *)[cell.exerciseViewArea viewWithTag:myImageViewTag];
    [imageView setImage:[UIImage imageNamed:[myArrayImages objectAtIndex:indexPath.row]]];
}

- (void)changeExerciseImage
{
    [myArrayImages replaceObjectAtIndex:4 withObject:@"testImage.png"];
    [myTableView reloadData]; //or just update that cell with reloadItemsAtIndexPaths
}

你的照片是怎么进入牢房的?在你发布的代码中,我看不到任何你称之为changeExerciseImage的地方。