简体   繁体   中英

indexPath.row not giving correct value in tableView custom cell iOS

I made custom cell with scrollView on 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;
}
UIScrollView *scrollView = (UIScrollView *)[cell viewWithTag:16];

Change above line with following.

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

i hope this works for you..

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM