2017-04-11 50 views
0

我有一個項目,人們在創建集合視圖項目時可以分配一個詞,並可以使用他們分配給集合視圖項目的詞彙在另一個視圖中搜索它,例如:某人創建項目並將其命名爲「牛奶」然後使用工作雜貨,以便以後可以搜索它。如何使用文本字段進行搜索?

我使用CoreData和我保存在集合視圖項目創造這個詞有一個屬性,我的代碼這究竟是怎麼查找指定的文本字段填寫在收集觀察室創建其中:

@IBAction func doneTran(_ sender: UIButton) { 

    if textFieldT.text != "" { 


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

     let task = Task(context: context) 

     task.name = textFieldT.text! 

     task.desc = descBox.text! 

     task.hashtag = hashtagField.text! 

     task.pid = pidStore 

     (UIApplication.shared.delegate as! AppDelegate).saveContext() 

     // Saves info to CoreData 
    } 

哈希標籤是存儲要搜索的術語的標籤。然後我有一個新的視圖控制器,可以使用hashtag字符串搜索項目。它使用textField和UIButton根據它們的hashtag屬性是否與textField文本匹配來重新加載視圖並顯示項目。這是我是如何實現它:

@IBOutlet weak var resultsColl: UICollectionView! 

@IBOutlet weak var textFieldFHash: UITextField! 

var taskTwo : [Task] = [] 

var activeCell : HashCollectionViewCell! 

var testHashAt = String() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.resultsColl.delegate = self 
    self.resultsColl.dataSource = self 

    testHashAt = textFieldFHash.text! 
} 


override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(animated) 

    getData() 
    resultsColl.reloadData() 

    testHashAt = textFieldFHash.text! 
} 

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return taskTwo.count 
} 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

    let taskTHash = taskTwo[indexPath.row] 

    let cellHash = collectionView.dequeueReusableCell(withReuseIdentifier: "nCell", for: indexPath) as! HashCollectionViewCell 

    if testHashAt == taskTHash.hashtag { 
     cellHash.labelFHash?.text = taskTHash.name 
    } 
    else { 
     print("blank cell?") 
    } 

    if cellHash.labelFHash.text == "" { 
     print("cell is nil") 
    } 

    cellHash.layer.cornerRadius = 25 

    return cellHash 
} 

@IBAction func searchHashBtn(_ sender: UIButton) { 
    getData() 
    resultsColl.reloadData() 
} 

func getData() { 

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

    do { 
     let fetchRequest: NSFetchRequest<Task> = Task.fetchRequest() 
     fetchRequest.predicate = NSPredicate.init(format: "hashtag = %@", testHashAt); 
     taskTwo = try context.fetch(fetchRequest) 
    } 

    catch { 
     print("Get Data failed") 
    } 

    // Fetches data. 
} 

也就是說在視圖控制器的所有代碼搜索,但是當我搜索的詞沒有出現。我不確定這是否與搜索按鈕沒有實際重新加載視圖以顯示單元格或一般搜索功能不起作用有關。有誰知道如何根據他們的hashtag屬性獲得正確的項目來顯示?

+0

的可能的複製[如何使的UITextField表現得像在斯威夫特一個UISearchBar?](http://stackoverflow.com/questions/26446376/how-to -make-的UITextField-行爲般-A-的UISearchBar功能於SWIFT) –

回答

0

這個下面的代碼試試:

import UIKit 

class TDSearchVC: UIViewController , UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate{ 


@IBOutlet var textSearch: UITextField! 
@IBOutlet var tblSearchResult: UITableView! 

var arrData : [String] = [] 
var arrFilterData : [String] = [] 
var isSearch : Bool! 


override func viewDidLoad() { 
    super.viewDidLoad() 

    // Do any additional setup after loading the view. 

    isSearch = false 

    arrData = ["Apple", "Banana", "Chikoo", "Brew", "Cherry", "Mango", "Lotus", "Peacock", "Temple", "Pine Apple","Glass", "Rose", "Church", "Computer", "Carrot"] 

} 

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

// MARK:- textfield 

public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool{ 

    var searchText = textField.text! + string 

    if string == "" { 
     searchText = (searchText as String).substring(to: searchText.index(before: searchText.endIndex)) 
    } 

    if searchText == "" { 
     isSearch = false 
     tblSearchResult.reloadData() 
    } 
    else{ 
     getSearchArrayContains(searchText) 
    } 

    return true 
} 

// Predicate to filter data 
func getSearchArrayContains(_ text : String) { 
    var predicate : NSPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", text) 
    arrFilterData = (arrData as NSArray).filtered(using: predicate) as! [String] 
    isSearch = true 
    tblSearchResult.reloadData() 
} 

// MARK:- TableView Delegates 
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ 

    if isSearch! { 
     return arrFilterData.count 
    } 
    else{ 

     return arrData.count 
    } 
} 

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

    var cell : UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell")! as UITableViewCell 

    if isSearch! { 
     cell.textLabel?.text = arrFilterData[indexPath.row] 
    } 
    else{ 

     cell.textLabel?.text = arrData[indexPath.row] 
    } 
    return cell 
} 
} 
相關問題