Reputation: 313
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;
}
Upvotes: 0
Views: 496
Reputation: 6363
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.
Upvotes: 0
Reputation: 462
UIScrollView *scrollView = (UIScrollView *)[cell viewWithTag:16];
Change above line with following.
UIScrollView *scrollView = (UIScrollView *)[cell viewWithTag:indexPath.row];
i hope this works for you..
Upvotes: 1