单击UITableview单元格内的search时出现错误的结果
在tableview的单元格中,我有一个button。 该button基本上打开单元格引用的文件。 这是工作正常,而search不上。 但是,search时,我似乎无法得到search结果数组中的正确的行。 这是我迄今为止所尝试的:在cellForRowAtIndexPath标记button。
myDownloadsBtn = (UIButton *) [cell viewWithTag:106];
行动设置如下:
[myDownloadsBtn addTarget:self action: @selector(downloadClicked:event:) forControlEvents:UIControlEventTouchUpInside];
然后在downloadClicked我有以下
NSSet *touches = [event allTouches]; UITouch *touch = [touches anyObject]; NSString *todoRow; CGPoint currentTouchPosition = [touch locationInView:self->tableview]; NSIndexPath *indexPath = [self->tableview indexPathForRowAtPoint: currentTouchPosition];
但是它给了我VISIBLE当前位置的indexPath,而不是search结果数组中行的位置。
此外,我的委托方法的didSelectRowAtIndexPath正常工作,没有search。 这只是单元格内的button。
我发现这个问题,但没有答案: 如何在iOS中search时获得选定的tableviewbutton值 。
其他问题涉及如何知道search时select的实际单元格,但这不是我的问题。
谢谢。
哟可以分配标签button
[myDownloadsBtn setTag:indexPath.row];
并在您的downloadClicked操作访问标签,因此被触摸/button点击行的索引
-(IBAction)myDownloadsBtn:(id)sender { UIButton *tappedBtn = (UIButton*)sender; Nslog("%d",tappedBtn.tag); // Get tag hence indexpath of tableview in which button is clicked. }
您需要使用search结果数组重新加载表格数据。
希望能帮助到你。 快乐编码!
为了在tebleview
特定索引上识别对象,你需要给你的特定indexpath.row
作为你的UIButton
标签。
这是最好的解决scheme。
aCell.yourButton.tag = indexPath.row [aCell.yourButton addTarget:self action: @selector(downloadClicked:event:) forControlEvents:UIControlEventTouchUpInside];
之后,你需要处理这个事件
-(IBAction)myDownloadsBtn:(id)sender { UIButton *tappedBtn = (UIButton*)sender; Nslog("%d",tappedBtn.tag); // it gives index of object related to your cell }
但是你所说的是在执行search之后你没有得到正确的结果。 我在想什么是从search结果后,你需要重新加载你的tableview
使用
[yourTableView reloadData]