UISearchBar查询通过数组不起作用:(

时间:2019-02-10 01:59:12

标签: swift xcode core-data predicate nsfetchrequest

enter image description here

我正在尝试使用核心数据构建一个待办事项列表,以持久化类别视图控制器项数组和相应类别项数组中的数据。我建立了实体之间的关系,因此当从根控制器中选择类别时,将对相应的项目列表执行搜索。一切正常。...但是,items控制器的过滤器未按预期工作,当在搜索栏中键入要搜索的所需项目时,无论要过滤哪个词,它都只会返回第一个项目。我不知道这件事可能在哪里,如果有人请帮忙,我将非常感激。

我已经尝试以我所知范围内的任何可能的方式修改Delegate方法,因此我对想法一无所知。

** ToDoListVC

导入UIKit 导入CoreData

ToDoListViewController类:UITableViewController {

var toDoItemArray = [ToDoItem]()
var selectedCategory : Category? {
    didSet{
        loadItems()
    }
}

@IBOutlet weak var searchBar: UISearchBar!

let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

override func viewDidLoad() {
    super.viewDidLoad()

    print (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask))

    searchBar.delegate = self

    let tapGesture = UITapGestureRecognizer(target: self, action: #selector (tableViewTapped))
    tableView.addGestureRecognizer(tapGesture)

    // Do any additional setup after loading the view, typically from a nib.

    tableView.register(UINib(nibName: "CustomToDoItemCell", bundle: nil), forCellReuseIdentifier: "CustomToDoItemCell")

}


//MARK - TableView DataSource Methods

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return toDoItemArray.count
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "CustomToDoItemCell", for: indexPath) as! CustomToDoItemCell

    let item = toDoItemArray[indexPath.row]

    cell.toDoBodyText?.text = item.itemText

    cell.toDoBodyText = UILabel(frame: CGRect(x: 16, y: 11, width: 160, height: 21))

    cell.accessoryType = item.doneStatus == true ? .checkmark : .none

    return cell

}

//MARK: - TableView Delegate Methods

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print(toDoItemArray[indexPath.row])

    //context.delete(toDoItemArray[indexPath.row])
    //toDoItemArray.remove(at: indexPath.row)

    toDoItemArray[indexPath.row].doneStatus = !toDoItemArray[indexPath.row].doneStatus

    self.saveItems()

    tableView.deselectRow(at: indexPath, animated: true)
}

//MARK: - Add New Items


@IBAction func addNewToDo(_ sender: UIBarButtonItem) {

    var textField = UITextField()

    let alert = UIAlertController(title: "Add A New Check-o Item", message: "", preferredStyle: .alert)

    let addNewAction = UIAlertAction(title: "Add New To Do", style: .default) { (addNewAction) in

        let newItem = ToDoItem(context: self.context)

        newItem.itemText = textField.text!

        newItem.doneStatus = false

        newItem.parentCategory = self.selectedCategory

        self.toDoItemArray.append(newItem)

        self.saveItems()

    }

    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (cancelAction) in

        self.dismiss(animated: true, completion: nil)
        self.tableView.reloadData()

    }

    alert.addTextField { (alertTextField) in

        alertTextField.placeholder = "Start Typing Your New To Do"
        textField = alertTextField //Extending the scope of the alert Text Field

    }

    alert.addAction(cancelAction)

    alert.addAction(addNewAction)

    present(alert, animated: true, completion: nil)

}


//MARK: - Creating & Reading Core Data


func saveItems () {

    do {

        try context.save()

    } catch {

        print ("Error saving context \(error)")

    }

    tableView.reloadData() //This method reloads data so the new item from the text field alert is added to the To Do Item Array
}

func loadItems (with  request : NSFetchRequest<ToDoItem> = ToDoItem.fetchRequest(), predicate : NSPredicate? = nil) {

    let categoryPredicate = NSPredicate(format: "parentCategory.categoryName MATCHES %@", selectedCategory!.categoryName!)

    if let additionalPredicate = predicate {
            request.predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [categoryPredicate, additionalPredicate])
    } else {
            request.predicate = categoryPredicate
    }

    do {
            toDoItemArray = try context.fetch(request)
    } catch {
            print ("Error Fetching Data From Context \(error)")
    }

        tableView.reloadData()
    }

}

// MARK:-搜索栏代表方法

扩展ToDoListViewController:UISearchBarDelegate {

@objc func tableViewTapped() {

    searchBar.endEditing(true)

}

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {

    let request : NSFetchRequest = ToDoItem.fetchRequest()

    let predicate = NSPredicate(format: "itemText CONTAINS[cd] %@", searchBar.text!)

    request.sortDescriptors = [NSSortDescriptor(key: "itemText", ascending: true)]

    loadItems(with: request, predicate: predicate)

}

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String){

    if searchBar.text?.count == 0 {

        loadItems()

        DispatchQueue.main.async {

            searchBar.resignFirstResponder()

        }
    }
}

}

** CategoryViewController

导入UIKit 导入CoreData

类别CategoryViewController:UITableViewController {

var categoryArray = [Category]()

let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

override func viewDidLoad() {
    super.viewDidLoad()

     print (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask))

    loadItems()

    tableView.register(UINib(nibName: "CustomToDoItemCell", bundle: nil), forCellReuseIdentifier: "CustomToDoItemCell")


}

//MARK: - TableView DataSource Methods

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return categoryArray.count

}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "CustomToDoItemCell", for: indexPath) as! CustomToDoItemCell
    let item = categoryArray[indexPath.row]

    cell.toDoBodyText?.text = item.categoryName
    cell.toDoBodyText = UILabel(frame: CGRect(x: 16, y: 11, width: 160, height: 21))

    return cell

}


//MARK: - Data Source Delegate Methods

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "goToItems", sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    let destinationVC = segue.destination as! ToDoListViewController
    if let indexPath = tableView.indexPathForSelectedRow {

        destinationVC.selectedCategory = categoryArray[indexPath.row]

    }

}


//MARK: - Add New Categories

@IBAction func addNewCategory(_ sender: UIBarButtonItem) {
    var textField = UITextField()
    let alert = UIAlertController(title: "Add New Check-o Category", message: "", preferredStyle: .alert)
    let addNewCategory = UIAlertAction(title: "Add New Category", style: .default) { (addNewCategory) in

        let newItem = Category(context: self.context)
        newItem.categoryName = textField.text!
        self.categoryArray.append(newItem)
        self.saveItems()
    }

    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (cancelAction) in

        self.dismiss(animated: true, completion: nil)
        self.tableView.reloadData()

    }

    alert.addTextField { (alertTextField) in

        alertTextField.placeholder = "Start Typing Your New Category"
        textField = alertTextField

    }

    alert.addAction(addNewCategory)
    alert.addAction(cancelAction)

    present(alert, animated: true, completion: nil)

}

//MARK: - TableView Manipulation Methods

func saveItems () {

    do {

        try context.save()

    } catch {

        print ("Error saving context \(error)")

    }

    tableView.reloadData() //This method reloads data so the new item from the text field alert is added to the To Do Item Array
}

func loadItems (with  request : NSFetchRequest<Category> = Category.fetchRequest()) {

    do {
        categoryArray = try context.fetch(request)
    } catch {
        print ("Error Fetching Data From Context \(error)")
    }

    tableView.reloadData()
}

}

希望能够从待办事项控制器中过滤搜索栏中键入的所有单词,并实际显示查询结果。

0 个答案:

没有答案