UITableView 中的 UIScrollView 中的 ContentOffset 问题

ContentOffset issue in UIScrollView in UITableView

我在 UITableView 的每个 UITableViewCell 中添加了一个 UIScrollView。

这是我的代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* CellIdentifier = @"Cell";
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    UIScrollView *propertyScrollView;
    if (cell == nil)
    {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        propertyScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, tableView.frameWidth, 100)];
        [propertyScrollView setContentSize:CGSizeMake(10*tableView.frameWidth, 100)];
        [propertyScrollView setPagingEnabled:YES];
        propertyScrollView.delegate = self;
        propertyScrollView.tag = indexPath.row;
        [[cell contentView] addSubview:propertyScrollView];
        propertyScrollView.backgroundColor = [UIColor blackColor];
        int pagingIndex = [[m_pagingIndexArray objectAtIndex:indexPath.row]intValue];
        [propertyScrollView setContentOffset:CGPointMake(pagingIndex*propertyScrollView.frameWidth, 0)];
        for(int i=0;i<10;i++)
        {
            UIButton *singleImage =  [[UIButton alloc]initWithFrame:CGRectMake(i*tableView.frameWidth, 0, propertyScrollView.frameWidth, propertyScrollView.frameHeight)];
        [propertyScrollView addSubview:singleImage];
        singleImage.titleLabel.text = [NSString stringWithFormat:@"%d",i];
        singleImage.titleLabel.textColor = [UIColor blackColor];
        singleImage.titleLabel.font = systemFontBoldTypeOfSize(20);
        [singleImage setImage:[horizentalImagesArray objectAtIndex:i] forState:UIControlStateNormal];
        singleImage.backgroundColor = [UIColor whiteColor];
    }
    else
    {
        propertyScrollView.tag = indexPath.row;
        int pagingIndex = [[m_pagingIndexArray objectAtIndex:indexPath.row]intValue];
        [propertyScrollView setContentOffset:CGPointMake(pagingIndex*propertyScrollView.frameWidth, 0)];
    }
    return cell;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    int pageIndex = scrollView.contentOffset.x/scrollView.frameWidth;
    [m_pagingIndexArray replaceObjectAtIndex:scrollView.tag withObject:[NSString stringWithFormat:@"%d",pageIndex]];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    m_pagingIndexArray = [[NSMutableArray alloc]init];
    for(int i=0;i<10;i++)
    {
        [m_pagingIndexArray addObject:@"0"];
    }
}

我在单个 UIScrollView(启用分页)中添加 10 个 UIButton。

问题是,如果我滚动任何一个 UITableViewCell 的滚动视图并移动到底部单元格,我可以看到其他一些 UITableViewCell 的滚动视图也滚动到该内容偏移点。

我希望我所有的 UITableview 单元格的 UIScrollView 独立滚动。我怎样才能实现它?请帮我解决这个问题。

如果单元格未被重用,您的代码是正确的,但由于单元格被重用,如果它们被重用,您需要为每个单元格的滚动视图设置内容偏移量。

你需要写一个else case -

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(cell == nil)
{
   // your code
}
else
{
   // reset the content offset of the scroll view as per your needs for the reused cells
}
return cell;
}

您将当前页码保留在 m_pagingIndexArray 中,并且所有单元格都使用相同的数组,因此假设它包含的页码为 3,那么它将适用于所有单元格.

如果像我在下面添加的那样被重复使用,您还需要为每个单元格重置每个按钮的文本和图像-

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = nil;
    UIScrollView *propertyScrollView;
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        propertyScrollView = [[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, tableView.frameWidth, 100)] autorelease];
        [propertyScrollView setContentSize:CGSizeMake(10*tableView.frameWidth, 100)];
        [propertyScrollView setPagingEnabled:YES];
        propertyScrollView.delegate = self;
        propertyScrollView.tag = indexPath.row;
        [[cell contentView] addSubview:propertyScrollView];
        propertyScrollView.backgroundColor = [UIColor blackColor];
        int pagingIndex = [[m_pagingIndexArray objectAtIndex:indexPath.row]intValue];
        [propertyScrollView setContentOffset:CGPointMake(pagingIndex*propertyScrollView.frameWidth, 0)];
        for(int i=0;i<10;i++)
        {
            UIButton *singleImage =  [[[UIButton alloc]initWithFrame:CGRectMake(i*tableView.frameWidth, 0, propertyScrollView.frameWidth, propertyScrollView.frameHeight)] autorelease];
        [propertyScrollView addSubview:singleImage];
        singleImage.titleLabel.text = [NSString stringWithFormat:@"%d",i];
        singleImage.titleLabel.textColor = [UIColor blackColor];
        singleImage.titleLabel.font = systemFontBoldTypeOfSize(20);
        [singleImage setImage:[horizentalImagesArray objectAtIndex:i] forState:UIControlStateNormal];
        singleImage.backgroundColor = [UIColor whiteColor];
    }

    return cell;
}