【问题标题】:indexPath of filtered tableView过滤后的tableView的indexPath
【发布时间】:2013-07-03 10:55:05
【问题描述】:

在我的项目中,我使用带有神奇记录的核心数据。

我的搜索栏工作正常,但搜索后如果我点击我的表格视图的第一行,我会得到未过滤的表格的详细视图:

所以如果我有 3 个名为 A、B、C 的对象并搜索 C,在我的表中我只看到 C,但如果我点击我会得到 A 的详细视图。

所以我尝试编写一个方法来获取正确的 indexPath:

   -(NSIndexPath*)giveMeSearchedObject:(UISearchBar*)searchBar showInRow: (int) row
{
     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"nome CONTAINS [cd] %@", searchBar];
    NSArray *arra = [Ricetta MR_findAllSortedBy:@"nome" ascending:YES withPredicate:predicate];
    Ricetta*ctn = arra[row];

    NSIndexPath* correctPath =arra[row];

return correctPath;

}

但是当我在 prepareForSegue 中调用这个方法来设置 indexPath 时,我不知道在 "showInRow:" 之后写了什么:

if (searching == YES) {

   NSIndexPath *indexPath = [[NSIndexPath alloc]init];

  indexPath = [[DataManager sharedClass]giveMeSearchedObject:self.mySearchBar.text showInRow:???;


 DetailViewController*dvc = segue.destinationViewController;
    dvc.indice =indexPath.row;

所以,也许方法不正确。

有人可以帮助我吗?

更新

@interface
@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
@end
@implementation

@synthesize fetchedResultsController;


 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

        if ([[segue identifier] isEqualToString:@"showDetail"]) {

    if (searching == YES) {

                UITableViewCell*cell = (UITableViewCell*)sender;
                NSIndexPath*indexPath = [self.tableView indexPathForCell:cell];
                Ricetta*ricetta = [self.fetchedResultsController objectAtIndexPath:indexPath];

                DetailViewController*dvc = segue.destinationViewController;
                dvc.indice =indexPath.row;

【问题讨论】:

  • 您的数组中是否有任何唯一标识符?
  • @Sunny 是的,当然
  • 根据该唯一标识符和当前选定的单元格(indexpath.row)获取原始索引值。
  • @Sonny 抱歉,但我是编程新手。可以举个例子吗?

标签: ios core-data uisearchbar segue magicalrecord


【解决方案1】:

showInRow 接受一个 int 所以使用

indexPath.row

【讨论】:

  • 我试过了,但是崩溃了。我想我不能使用“indexPath”来定义“indexPath”
  • 嗯,你是从 prepareForSegue 运行的,所以你需要引用你当前的 indexPath,不要完全按照我写的方式使用它。创建一个属性来保存当前 selectedIndex 然后使用它。该索引的行是您将过滤数组以获取适当对象的行。
  • 抱歉,我是编程新手。你能发布一些示例代码吗?
【解决方案2】:

您的代码有很多问题。例如,您的方法giveMeSearchedObject 应该返回一个索引路径,而是返回一个Ricetta。最好忘记它。

本应让“初学者”“更轻松”的第 3 方框架的问题在于,它们的文档通常很糟糕。通常,它们在简化或抽象方面几乎没有添加 - 这包括 MR。在您的情况下,如果不使用普通的香草 NSFetchedResultsController,您将一无所获,您可以使用搜索词进行更新。

如果您使用情节提要并在prepareForSegue 中捕获单元格选择,则必须通过sender 这是一个表格视图单元格来访问托管对象:

UITableViewCell *cell = (UITableViewCell*)sender;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 
Ricetta *ricetta = [self.fetchedResultsController objectAtIndexPath:indexPath]; 

【讨论】:

  • 试过你的方法,但不起作用。我错了吗?(请参阅更新)
  • 附言。我知道 MR 没有足够的文档记录,但是当您找到 MR 的方法时,一切都会变得更容易。
  • 是的,确保此代码不起作用。也许还有一些概念上的挑战......给你的细节视图控制器一个带有Ricetta实体的属性。它不应该对数据一无所知,只知道它需要知道什么来显示配方。它被称为“encapsulation”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多