我制作了自定义 cell
,在 cell
的右侧使用 scrollView
,在左侧使用 imageView
。现在,当我点击 imageView
时,我得到了 indexPath.row
的正确值,但是当我点击 scrollView indexPath.row
时,返回的是上次的值被点击的图像。有什么解决方法的建议吗?
编辑:
- (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];
将上面的行更改为以下行。
UIScrollView *scrollView = (UIScrollView *)[cell viewWithTag:indexPath.row];
我希望这对你有用..
关于ios - indexPath.row 在 tableView 自定义单元格 iOS 中没有给出正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20516906/