【问题标题】:clicking button within UITableview cell gives wrong result while search单击 UITableview 单元格中的按钮在搜索时给出错误的结果
【发布时间】:2017-12-12 14:48:46
【问题描述】:

在表格视图的单元格中,我有一个按钮。该按钮基本上打开了单元格引用的文件。这在搜索未开启时工作正常。但是,当搜索打开时,我似乎无法在搜索结果数组中找到正确的行。 这是我到目前为止所尝试的: 在 cellForRowAtIndexPath 中我标记了按钮。

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];

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

另外,我的didSelectRowAtIndexPath 委托方法无论有没有搜索都可以正常工作。它仅适用于单元格内的按钮。

我找到了这个问题,但没有答案: how to get selected tableview button value while search in iOS.

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

谢谢。

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    你可以将标签分配给按钮

    [myDownloadsBtn setTag:indexPath.row];
    

    并在您的 downloadClicked 操作中访问标签,从而访问被触摸/单击按钮的行的索引

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

    您需要重新加载带有搜索结果数组的表格数据。

    希望对您有所帮助。快乐编码!

    【讨论】:

    • 谢谢!!很抱歉这么晚才回复,但这是完美的!
    【解决方案2】:

    为了在tebleview 的特定索引上识别对象,您需要将您的特定indexpath.row 作为标签提供给您的UIButton

    这是最好的方法。

    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
     }
    

    但是你说的是你在执行搜索后没有得到正确的结果。 我在想的是在获得搜索结果后,您需要使用

    重新加载您的tableview
    [yourTableView reloadData]
    

    【讨论】:

    • 非常感谢!我得到了正确的搜索结果。问题是在搜索结果数组中按下按钮时。标记是正确的。输入“cell.myDownloadsBtn.tag = indexpath.row”时出现错误。如果它只是 myDownloadsBtn.tag,它就可以工作。
    猜你喜欢
    • 1970-01-01
    • 2016-02-06
    • 1970-01-01
    • 2014-11-12
    • 1970-01-01
    • 1970-01-01
    • 2011-04-22
    • 2015-01-30
    • 1970-01-01
    相关资源
    最近更新 更多