【问题标题】:Getting the self.tableview indexpath from UISearchDisplayController tableView indexpath从 UISearchDisplayController tableView indexpath 获取 self.tableview indexpath
【发布时间】:2011-06-29 22:07:07
【问题描述】:

所以我有一个详细视图,需要通过从 self.tableview 中选定单元格的 indexpath 中获取一个索引整数来显示信息,我也一直在将 NSFetchedResultsController 用于 self.tableview。我还实现了一个 UISearchDisplayController 来进行搜索。

问题: 如何将 UISearchDisplayController tableview 中选择的 indexPath 转换为原始 self.tableview 的 indexpath?还是我需要设置一个 NSArray 实例并循环遍历它以找出索引?最有效的方法是什么?

代码如下:

-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
    NSLog(@"display project details");
    if (tableView == self.table) {
        [parentController projectSelectedFromList:indexPath.row];
    }else{
        NSInteger index;
        //what to put here? To get the indexPath of the self.table from search tableview 
        [parentController projectSelectedFromList:index];
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

【问题讨论】:

    标签: iphone objective-c cocoa-touch uitableview


    【解决方案1】:

    假设没有重复,

    id selectedObject = [searchArray objectAtIndex:indexPath.row];
    index = [sourceArray indexOfObject:selectedObject];
    

    想法很简单。获取搜索数组的选定对象,因为它将直接映射到索引路径的行。然后在源或主数组中搜索对象的索引。这应该会给你你想要的索引。

    【讨论】:

    • 谢谢。这基本上就是我所做的。诀窍是找出 NSFetchedResultsController 的 indexPathForObject 方法。 :)
    【解决方案2】:

    看看你在tableView 数据源(通常是tableViewController 对象)的tableView:cellForRowAtIndexPath: 中有什么。假设您对是否使用标准 tableView 或搜索 tableView 进行了类似的检查 - 如果不是,那么您的搜索过滤器肯定不会有效 - 您只需要在您的tableView:didSelectRowAtIndexPath:.

    如果我在这里遗漏了重点,那么很抱歉……您可能需要在问题中多说一点。

    【讨论】:

    • 感谢您的输入,但我确实让我的搜索表视图正常工作,并且正在寻找一种将搜索表视图中选定的 indexPath 转换为原始 self.tableview 的索引路径的方法。 :)
    【解决方案3】:

    我找到了解决办法:

    -(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
    NSLog(@"display project details");
    if (tableView == self.table) {
        [parentController projectSelectedFromList:indexPath.row];
        NSLog(@"indexpath at orignal tableview is: %@", [indexPath description]);
    }else{
        NSIndexPath *indexPathForOriginal = [resultsController indexPathForObject: [self.filteredResults objectAtIndex:indexPath.row]];
        NSInteger index = indexPathForOriginal.row;
        [parentController projectSelectedFromList:index];
        NSLog(@"indexpath at search tableview is: %@", [indexPathForOriginal description]);
    
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    

    }

    【讨论】:

      【解决方案4】:

      Swift 版本的@Deepak Danduprolu 答案:

      override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
          var selectedObject = searchedArray[indexPath.row]
          var index = OrignalArray.index(of: selectedObject)
          print("index", index) // This will give you index of selected array from the original array
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多