我有一个自定义tableView单元格的tableview。 我在自定义tableViewCell上放置了一个UIImageview。当我按或触摸imageView时,我想根据选定的索引从网址下载一些图像,我将把它作为字符串传递给它(selectedIndex)。
当我第一次触摸图像视图时,我没有获得所选的索引路径。但是当我第一次选择单元格然后我按下图像视图时,那时我得到所选的索引路径。我在互联网上搜索过,但没有找到任何合适的解决方案。下面是我的代码,我在cellForRowAtIndexPath方法中将UILongPressRecognizer分配给UIImageView。
- (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视图,我就会得到索引。 任何人都可以告诉我,我在这里做错了什么。任何帮助表示赞赏。
修改
我稍微改变了我的代码。我没有使用UIImageView和UILongPressGestureRecognizer,而是使用UIButton。按下该按钮,我试图获取按钮所在的单元格的索引路径。下面是我的新代码。
- (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]:无法识别的选择器发送到实例 任何帮助或建议表示赞赏。
答案 0 :(得分:0)
我注意到你已经在imageView中恢复了indexPath.row,所以你需要做的只是在@selector(tapDetected12)
中从imageView获取indexPath.row。在您执行此操作之前,还有一件事要做:用@selector(tapDetected12)
替换@selector(tapDetected12:)
新参数是UIGestureRecognizer实例,从中可以获得用户长按{{1}的图片视图}的属性UIGestureRecognizer
。
的样品强>
@property(nullable, nonatomic,readonly) UIView *view
的快照强>
您可以注意到我先选择第6行(标签文本为07),当我点击第3行(标签文本为04)时,NSLog输出为- (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);
}
}
答案 1 :(得分:0)
试试这个:
- (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.
}