搜索栏tableview数组索引超出范围

时间:2016-01-17 18:03:55

标签: ios swift uitableview search

我试图在我的Tableview中创建搜索栏,但它没有给我过滤结果,表格为空

  

致命错误:数组索引超出范围

我认为Tableview更新功能或filterContent出了问题,但无法理解。有人可以向我解释一下吗?

        var find: [List] = []        //my core data file
        var searchResult: [List] = [] //my core data file

        let context: NSManagedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
        var detailViewController: MainVC? = nil
        var frc : NSFetchedResultsController = NSFetchedResultsController()

        var searchController: UISearchController!

        func getFetchResultsController() ->NSFetchedResultsController {
            frc = NSFetchedResultsController(fetchRequest: listFetchRequest(), managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)

            return frc
        }

        func listFetchRequest() -> NSFetchRequest{
            let fetchRequest = NSFetchRequest (entityName: "List")
            let sortDescriptor = NSSortDescriptor(key: "item", ascending: true)
            fetchRequest.sortDescriptors = [sortDescriptor]
            return fetchRequest
        }

        override func viewDidLoad() {
            super.viewDidLoad()

            frc = getFetchResultsController()
            frc.delegate = self
            do {
                try frc.performFetch()
            } catch {}
            if let split = self.splitViewController {
                let controllers = split.viewControllers
                self.detailViewController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? MainVC
            }
            searchController = UISearchController(searchResultsController: nil)
            tableView.tableHeaderView = searchController.searchBar
            searchController.searchResultsUpdater = self
            searchController.dimsBackgroundDuringPresentation = false
        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }

        func controllerDidChangeContent(controller: NSFetchedResultsController) {
            tableView.reloadData()
        }


        // MARK: - Table view data source

        override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
            let numberOfSections = frc.sections?.count
            return numberOfSections!
        }

        override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            if searchController.active {
                return searchResult.count
            } else {
                //return find.count
                let numberOfRowsInSection = frc.sections?[section].numberOfObjects
                return numberOfRowsInSection!
            }

            // #warning Incomplete implementation, return the number of rows

        }


        override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {      
            let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)       
            let search = (searchController.active) ? searchResult[indexPath.row] : frc.objectAtIndexPath(indexPath) as! List
       print(search)
        //Configure the cell..
       cell.textLabel!.text = search.item
        let qty = search.qty
        let address = search.address

        cell.detailTextLabel?.text = "Dukan Sayi: \(qty)\(address)"
        return cell
}
        override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
            if searchController.active {
                return false
            } else {
                return true
            }
        }
        override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

            let managedObject : NSManagedObject = frc.objectAtIndexPath(indexPath) as! NSManagedObject

            context.deleteObject(managedObject)

            do {
                try context.save()
            } catch {}
        }
 func updateSearchResultsForSearchController(searchController: UISearchController) {
      if let searchText = searchController.searchBar.text {
        filterContent(searchText)
         print(searchText)
        tableView.reloadData()
    }
  }
  func filterContent(searchText: String) {
   searchResult = find!.filter({ ( a: List) -> Bool in
        let nameMatch = a.item.rangeOfString(searchText, options:   NSStringCompareOptions.CaseInsensitiveSearch)
        return nameMatch != nil
        })

   }

0 个答案:

没有答案