ios - indexPath.row 在 tableView 自定义单元格 iOS 中没有给出正确的值

标签 ios iphone ipad uitableview uiscrollview

我制作了自定义 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/

相关文章:

ios 快速搜索栏颜色更改

ios - Xcode、Swift - 单击时获取 tableViewCell 按钮的行索引

ios - iOS 项目的 XCode 8 中的 CMake 和代码签名

iphone - 由于在 CGContext 中重叠,图像显示不当

iphone - 如何更新 iOS 应用程序(文件除外)

ios - 如何在 iOS 7 中获取设备或 iPad 的唯一编号?

javascript - var 在 Foundation.util.touch.js 中未定义

ios - 自定义按钮在AVPlayer iOS 11中不起作用,但在iOS 11以下完全可以工作吗?

iphone - 是否有使用所有 NSURLConnection 委托(delegate)方法的完整示例?

ipad - 如何改变 iPad 模拟器的方向?