簡體   English   中英

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都可以獨立滾動。 我該如何實現? 請幫助我解決此問題。

您將當前頁碼保留在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;
}

如果單元格沒有被重用,您的代碼是正確的,但是當單元格被重用時,您需要為每個單元格的滾動視圖設置內容偏移(如果它們被重用)。

您需要編寫其他情況-

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM