搜索后的prepareForSegue方法

时间:2017-06-13 07:54:57

标签: ios objective-c arrays segue

我在我的项目中实现了搜索功能并且工作正常,但问题是我不知道如何在搜索后传递detailViewController的正确数据。

我的搜索方法如下:

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", searchText];
    NSMutableArray *filteredArray = [NSMutableArray arrayWithArray:[self.allItems filteredArrayUsingPredicate:predicate]];
    self.sortedArray = filteredArray;

    [self.tableView reloadData];

}

prepareForSegue方法如下所示:

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

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

        NSIndexPath *path = [self.tableView indexPathForSelectedRow];

        EvielasDetailViewController *detailViewController = [segue destinationViewController];

        detailViewController.eVielas = [[Evielas alloc] initWithIndex:path.row];

    }
}

我已经创建了单独的类来处理传递给detailViewController的数据,如下所示:

- (instancetype) initWithIndex:(NSUInteger) index {

    self = [super init];
    if (self) {
        EvielasLibrary *eVielasLibrary = [[EvielasLibrary alloc] init];
        _library = eVielasLibrary.library;

        NSDictionary *eVielasDictionary = _library[index];

        _eVielasNumurs = [eVielasDictionary objectForKey:kEviela];
        _eVielasNosaukums = [eVielasDictionary objectForKey:kNosaukums];
        _eVielasIespPielietojums = [eVielasDictionary objectForKey:kIespPielietojums];
        _eVielasIespIedarbiba= [eVielasDictionary objectForKey:kIespIedarbiba];

    }
    return self;
}

我的数据结构如下:

- (instancetype)init
{
    self = [super init];
    if (self) {
        _library = @[@{kEviela : @"E100",
                           kNosaukums : @"Kurkumīns",
                           kIespPielietojums : @"Krāsviela",
                           kIespIedarbiba : @"Drošs"},
                         @{kEviela : @"E101",
                           kNosaukums : @"Riboflavīns",
                           kIespPielietojums : @"-",
                           kIespIedarbiba : @"Drošs"},
                         @{kEviela : @"E102",
                           kNosaukums : @"Tartrazīns",
                           kIespPielietojums : @"Konditorejas izstrādājumi ,atspirdzinošie dzērieni, augļu sulas, marinēti dārzeņi, marcipāns, sausās zupas, siera cepumi, saldā kukurūza. \nLietošana citur: kosmētika, vilnas un zīda krāsa.",
                           kIespIedarbiba : @"Drošs"}...

问题在于,当我不搜索数据时,一切正常,因为indexPath与Array匹配,但是一旦我开始搜索索引就不匹配,detailViewController会向我显示错误的细节。

提前致谢!

4 个答案:

答案 0 :(得分:0)

我没有看到您传递给详细视图控制器的任何数据。为此,在detailVC中创建一个全局对象,并在prepareForSegue方法中分配它,在viewcontroller实例的帮助下访问它并传递它。

答案 1 :(得分:0)

然后你必须在触发segue之前从 UITableViewDelegate didSelect 方法全局存储值,然后在detailVC中传递它。

答案 2 :(得分:0)

正如@priyam建议的那样,试试这个:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  self.globalVar = self.sortedArray[indexPath.row]
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  if ([segue.identifier isEqualToString:@"showEvielasDetails"]) {
    EvielasDetailViewController *detailViewController = [segue destinationViewController];
    detailViewController.eVielas = //create Evielas object using self.globalVar

  }
}

答案 3 :(得分:0)

在为segue提供标识符后使用以下代码:

 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        let dataModel = DataModel()
        dataModel.addOnArray = []
        dataModel.dataDict = mainArray[indexPath.section] as! [String : String]
        self.performSegue(withIdentifier: "gotoDetailVC", sender: dataModel)

}


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "gotoDetailVC" {
        let detailVC = segue.destination as! ExpandDetailViewController
        detailVC.data = sender as! DataModel
    }
}