【发布时间】: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