0

I made custom cell with scrollViewon right side of the cell and imageView on left side. Now when I click on imageView I get correct value for indexPath.row, but when I click on scrollView indexPath.row is returning value from last image that was clicked. Any suggestions how to fix this?

EDIT:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *hlCellID = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:hlCellID];
    if(cell == nil) {
        cell =  [[UITableViewCell alloc]
               initWithStyle:UITableViewCellStyleDefault reuseIdentifier:hlCellID];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    UIScrollView *scrollView = (UIScrollView *)[cell viewWithTag:16];
    scrollView.backgroundColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0];
    scrollView.delegate = self;
    scrollView.scrollEnabled = YES;
    scrollView.contentSize =   CGSizeMake(scrollView.frame.size.width,scrollView.frame.size.height);
    [scrollView setShowsHorizontalScrollIndicator:NO];
    [scrollView setShowsVerticalScrollIndicator:NO];

    int i=0;
    for(i=0;i<15;i++){
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0+i*100, 0, 100, 100)];
        label.text = @"HELLO";
        label.textColor = [UIColor blackColor];
        label.backgroundColor = [UIColor clearColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont fontWithName:@"ArialMT" size:18];
        [scrollView addSubview:label];
        scrollView.contentSize = CGSizeMake(scrollView.frame.size.width+i*label.frame.size.width,scrolView.frame.size.height);
    }
    return cell;
}
lugonja
  • 303
  • 4
  • 15

2 Answers2

1
UIScrollView *scrollView = (UIScrollView *)[cell viewWithTag:16];

Change above line with following.

UIScrollView *scrollView = (UIScrollView *)[cell viewWithTag:indexPath.row];

i hope this works for you..

Maulik Kundaliya
  • 452
  • 3
  • 17
  • Tag is value that is setted in storyboard so I can identify my custom cell and scrollView in it. indexPath.row is not constant value. – lugonja Dec 11 '13 at 10:50
  • but please tell me, what are you trying to achieve ? – Maulik Kundaliya Dec 11 '13 at 10:54
  • I am trying to make EPG and I need Program Guide on right side that can slide left and right. I need to set all scrollViews in tableView to slide when I slide one of them, so I need indexPath.row of that cell in scrollViewDidScroll method, and I need to set rest of cells to make contentOffset same as that cell. – lugonja Dec 11 '13 at 11:00
  • then you can get tag of scrollview with using indexPath.row and you can do whatever you want. – Maulik Kundaliya Dec 11 '13 at 11:04
  • I know that, but indexPath.row is not returning correct value of selected row when i click on scrollView inside cell, and when I click outside of scrollView it works correctly. – lugonja Dec 11 '13 at 11:07
  • i think following link may helps you. http://stackoverflow.com/questions/6636844/uiscrollview-inside-uitableviewcell-touch-detect – Maulik Kundaliya Dec 11 '13 at 11:14
  • http://stackoverflow.com/questions/19185446/horizontal-uiscrollview-inside-custom-uitableviewcell-using-ib-storyboard-no – Maulik Kundaliya Dec 11 '13 at 11:15
0

You should look at this doc reference: responder chain

Basically your event doesn't goes uphill the responder chain because scroll view captures it. I don't know what you are trying to achieve but having scrollview inside a scrollview(because tables are scrollviews by their nature) is bad idea and is really hard to achieve.

hris.to
  • 6,235
  • 3
  • 46
  • 55
  • I am making EPG and I need picture of program on left side, and Program Guide on right side that can slide left and right. Table view itself can't achieve that. – lugonja Dec 11 '13 at 10:53