搜索时单击 UITableview 单元格中的按钮会给出错误的结果 [英] clicking button within UITableview cell gives wrong result while search

查看:22
本文介绍了搜索时单击 UITableview 单元格中的按钮会给出错误的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 tableview 的单元格中,我有一个按钮.该按钮基本上打开单元格引用的文件.这在搜索未开启时工作正常.但是在搜索过程中,我似乎无法在搜索结果数组中获得正确的行.这是我迄今为止尝试过的:在 cellForRowAtIndexPath 中我标记了按钮.

Within the cells of a tableview I have a button. The button basically opens the file that the cell refers to. This is working fine while the search is not on. But while the search is on, I can't seem to get the correct row within the search results array. This is what I have tried so far: in the cellForRowAtIndexPath I tagged the button.

myDownloadsBtn = (UIButton *) [cell viewWithTag:106];

动作设置如下:

[myDownloadsBtn addTarget:self action: @selector(downloadClicked:event:) forControlEvents:UIControlEventTouchUpInside];

然后在下载点击我有以下内容

then in downloadClicked I have the following

    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    NSString *todoRow;
    CGPoint currentTouchPosition = [touch locationInView:self->tableview];
    NSIndexPath *indexPath = [self->tableview indexPathForRowAtPoint: currentTouchPosition];

但它给了我可见当前位置的 indexPath,而不是搜索结果数组中行的位置.

but it gives me the indexPath of the VISIBLE current position, not the position of the row within the search results array.

此外,我的didSelectRowAtIndexPath 委托方法在搜索和不搜索的情况下都可以正常工作.它仅用于单元格内的按钮.

Also, my delegate method ofdidSelectRowAtIndexPath works fine with and without the search. It is just for buttons within the cells.

我找到了这个问题,但没有答案:如何在搜索时获取选定的tableview按钮值iOS.

I have found this question, but there is no answer: how to get selected tableview button value while search in iOS.

其他问题涉及如何知道搜索时选择的实际单元格,但这不是我的问题.

the other questions deal with how to know the actual cell selected when searching, but that is not my problem.

谢谢.

推荐答案

Yo can assign tag to button as

Yo can assign tag to button as

[myDownloadsBtn setTag:indexPath.row];

并在您的 downloadClicked 操作中访问标签,因此是被触摸/点击按钮的行的索引

and in your downloadClicked action access the tag hence the index of the row that is being touched / button clicked

-(IBAction)myDownloadsBtn:(id)sender
 {
   UIButton *tappedBtn = (UIButton*)sender;
   Nslog("%d",tappedBtn.tag); // Get tag hence indexpath of tableview in which button is clicked. 
  }

您需要使用搜索结果数组重新加载表格数据.

希望有帮助.快乐编码!!

Hope it helps. Happy Coding!!

这篇关于搜索时单击 UITableview 单元格中的按钮会给出错误的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆