0

我在 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 独立滚动。我怎样才能实现它?请帮我解决这个问题。

4

2 回答 2

1

您将当前页码保存在 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;
}
于 2015-03-24T08:01:45.950 回答
0

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

你需要写一个 else 案例 -

- (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;
}
于 2015-03-24T05:58:39.770 回答