【问题标题】:How to get the selected index path for UIImageView object present on a UITableVIewCell of UITableView?如何获取 UITableView 的 UITableVIewCell 上存在的 UIImageView 对象的选定索引路径?
【发布时间】:2016-06-24 14:11:05
【问题描述】:

我有一个包含自定义 tableView 单元格的 tableview。 我在自定义 tableViewCell 上放置了一个 UIImageview。当我按下或触摸 imageView 时,我想根据选定的索引从 url 下载一些图像,我将把它(selectedIndex)作为字符串传递给 url。

当我第一次触摸图像视图时会发生什么,我没有得到选定的索引路径。但是当我第一次选择单元格然后我按下图像视图时,那时我得到了选定的索引路径。我在互联网上搜索了它,但没有找到任何合适的解决方案。下面是我的代码,我在 cellForRowAtIndexPath 方法中为 UIImageView 分配了一个 UILongPressRecognizer。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


cell.imgvDownload.tag=indexPath.row;
    singleTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected12)];

    [cell.imgvDownload setUserInteractionEnabled:YES];
    [singleTap.delegate self];
    [singleTap setMinimumPressDuration:0.01f];
    [cell.imgvDownload addGestureRecognizer:singleTap];

return cell;
}

问题在于 UIImageview 能够获取所选行的所选索引路径。但是,如果我首先从 tableView 中选择任何单元格(行)然后按 UIImage 视图,我会得到索引。 谁能告诉我我在这里做错了什么。任何帮助表示赞赏。

编辑

我稍微更改了我的代码。我使用的是 UIButton,而不是使用 UIImageView 和 UILongPressGestureRecognizer。在按下该按钮时,我试图获取按钮所在单元格的索引路径。下面是我的新代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

cell.btnDownload.tag=indexPath.row;
return cell;
}

而按钮按下动作方法是-

-(IBAction)btnDownload:(id)sender{

UIView *sourceView = [sender view];
if ([sourceView isKindOfClass:[UIButton class]]) {
    NSLog(@"row is %ld", sourceView.tag);
}

并且在选择方法上,我正在像这样存储选定的索引路径-

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
selectedIndex =[NSString stringWithFormat:@"%@%@",[[_shopDArrayFetched objectAtIndex:indexPath.section]valueForKey:@"ctid"],[[_shopDArrayFetched objectAtIndex:indexPath.section]valueForKey:@"scid"]];
    NSLog(@"selectedIndex %@",selectedIndex);
}

我希望在我无法获得的按钮单击上获得此索引。
此外,该应用程序正在在线崩溃- UIView *sourceView = [sender view];说-[UIButton view]:发送到实例的无法识别的选择器 感谢您提供任何帮助或建议。

【问题讨论】:

  • 如果你只想点击,那你为什么用UILongPressGesture而不是UITapGesture
  • 我确实使用 UITapGesture 尝试过,但没有任何变化。

标签: ios uiimageview tags nsindexpath uiatableview


【解决方案1】:

我注意到你已经在 imageView 中恢复了 indexPath.row,所以你需要做的就是在@selector(tapDetected12)get indexPath.row from imageView。在你这样做之前,还有一件事要做:用@selector(tapDetected12:) 替换你的@selector(tapDetected12) 新参数是一个UIGestureRecognizer 实例,从中你可以通过UIGestureRecognizer 的属性@987654327 获得用户长按的图像视图@。

样品

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseid" forIndexPath:indexPath];

    cell.textLabel.text = self.datas[indexPath.row];
    cell.imageView.image = [UIImage imageNamed:@"someicon"];
    cell.imageView.tag = indexPath.row;
    UILongPressGestureRecognizer *singleTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected12:)];

    [cell.imageView setUserInteractionEnabled:YES];
    [singleTap.delegate self];
    [singleTap setMinimumPressDuration:0.01f];
    [cell.imageView addGestureRecognizer:singleTap];

    return cell;
}

- (void)tapDetected12:(UIGestureRecognizer *)gestureRecognizer {
    UIView *sourceView = gestureRecognizer.view;
    if ([sourceView isKindOfClass:[UIImageView class]]) {
        NSLog(@"row is %ld", sourceView.tag);
    }
}


快照
你可以注意到我首先选择了第 6 行(标签文本为 07),当我点击第 3 行(标签文本为 04)时,NSLog 输出为3

【讨论】:

  • 仍然无法获取特定 UITableViewCell 上图像视图的索引路径。帮帮我
  • @ammy1 请更新更多细节,否则我帮不了你。 sourceView.tag 的值是多少?以及什么样的[sourceView class]
  • @ammy1 为什么是UIView *sourceView = [sender view];sender 是您的btnDownload 的一个实例,因此使用UIView *sourceView = (UIView *)sender; 而不是UIView *sourceView = [sender view];。另一个问题,你期望的发送者是什么样的类?
  • 我对发件人类了解不多,因为我是 iOS 新手。我正在做的是对于这个表格视图,我已经为表格视图中的每一行(单元格)分配了一些变量。当我在表格视图中选择任何行时,我会得到该单元格的特定值。例如,如果我选择第一个单元格,我会得到选定的索引为-1001。如果我选择第二个单元格,我会得到选定的索引为 1002 ......等等。这些值我无法单击 tableview 单元格上的按钮。我应该在这里做什么?
  • @ammy1 我已经在示例代码中实现了您的要求。戴安娜普罗丹的另一个答案也是正确的,我不知道你有什么困惑
【解决方案2】:

试试这个:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
          // ...

          cell.imgvDownload.userInteractionEnabled = YES;
          UITapGestureRecognizer *imageTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(imageTap:)];
          [imageTapRecognizer setDelegate:self];
          [cell.imgvDownload addGestureRecognizer:imageTapRecognizer];
          cell.imgvDownload.tag = indexPath.row;

          return cell;
    }

    - (void)imageTap:(UITapGestureRecognizer*)sender
    {
          UIView *view = sender.view;
          NSLog(@"%ld", (long)view.tag); //By tag, you can find out where you had tapped.
    }

【讨论】:

  • 我试过你的建议,但还是不行。请帮帮我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多