0

I've added a UIScrollView in each UITableViewCell of a UITableView.

This is my code.

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

I'm adding 10 UIButtons in a single UIScrollView(Paging Enabled).

The problem is, if I scroll any one of the UITableViewCell's scrollview and move to bottom cells, I can see some other UITableViewCell's scrollviews also scrolled to that content offset point.

I want all my UITableview cell's UIScrollView to scroll independently. How can I achieve it? Please help me to sort out this issue.

Skywalker
  • 1,590
  • 1
  • 18
  • 36
Nazik
  • 8,696
  • 27
  • 77
  • 123
  • The scroll view's offset is just like any other property of a cell. You need to set it for every cell in cellForRowAtIndexPath because of cell reuse. That means you need to store this value in your model. – rdelmar Mar 24 '15 at 05:51
  • Your correct contentSize would be `10*tableView.frameWidth + propertyScrollView.frameWidth` and about scrolling, I think cells are getting overridden, you can reset the contentoffset as soon as your scroll the tableview. That might solve your problem. – iphonic Mar 24 '15 at 05:57
  • @rdelmar, I had made some changes as u suggested. Still it results the same. Pl correct me what I did wrong in the above code. – Nazik Mar 24 '15 at 07:22

2 Answers2

1

You are keeping the current page number in m_pagingIndexArray, and the same array is used for all the cells, so suppose if it contains the page number as 3, then it will be applicable for all the cells.

Also you need to reset the text and image of each button for each cell if it's being reused as I have added below-

- (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;
}
Sanjay Mohnani
  • 5,947
  • 30
  • 46
  • I apologise for late reply. It's still not working for me. Could u pl suggest any other idea than the above? – Nazik Mar 24 '15 at 10:21
  • are you using the m_pagingIndexArray for all the scrollview's? – Sanjay Mohnani Mar 24 '15 at 10:31
  • Thanks friend. I just removed cell from superview and created the cell newly for every row. It worked fine. I'd edited the answer. You can revert the edit, if you think answer is not correct way to solve the issue. I have one doubt. Will it create memory issue? – Nazik Mar 24 '15 at 13:20
  • yes, ARC won't allow autorelease. If you are using ARC than just remove the autorelease messages from above code. – Sanjay Mohnani Mar 25 '15 at 04:16
0

Your code is correct for the case if cell is not reused, but as the cells are reused you need to set the content offset for scroll view of each cell if they are reused.

You need to write an 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;
}
Sanjay Mohnani
  • 5,947
  • 30
  • 46