tableview上的搜索栏不起作用

时间:2016-10-27 17:21:24

标签: uitableview search swift3

我是编码的绝对初学者

我使用xcode8和swift3

我有一个用json数据填充的tableview ...

现在我想用搜索栏过滤结果。

我到目前为止,但搜索栏没有做任何事情......

let session = URLSession.shared
    let task = session.dataTask(with: url as URL) {(NSData, response, error) -> Void in
        do {
            let urlContent = NSData
            let records = try JSONSerialization.jsonObject(with: urlContent!, options:[]) as! [[String:Any]]

            AppDelegate.notes.removeAll()

            for record in records   {

                print(record["name"] as! String)

                let note = StructNote()
                if record["uniname"] != nil {
                note.uniname = record["uniname"] as! String
                }
                if record["place"] != nil {
                note.place = record["place"] as! String
                }
                if record["time"] != nil {
                note.intime = record["time"] as! String
                }

            }




            let concurrentQueue = DispatchQueue(label: "queuename", attributes: .concurrent)
            concurrentQueue.sync {
                self.tableViewNote.reloadData()

            }
        }
        catch {
            print("Json Error")
        }
    }
    task.resume()
}


override func viewDidLoad() {
    super.viewDidLoad()


    //----------- Searchbar ----------------------

    tableViewNote.delegate = self
    tableViewNote.dataSource = self
    self.resultSearchController = ({
        let controller = UISearchController(searchResultsController: nil)
        controller.searchResultsUpdater = self
        controller.dimsBackgroundDuringPresentation = false
        controller.searchBar.sizeToFit()
        controller.searchBar.barStyle = UIBarStyle.black
        controller.searchBar.barTintColor = UIColor.white
        controller.searchBar.backgroundColor = UIColor.clear
        self.tableViewNote.tableHeaderView = controller.searchBar
        return controller
    })()
    self.tableViewNote.reloadData()

    //----------- Searchbar ----------------------


}   // here is the end of viewdidload



    //----------- Searchbar ----------------------

    func updateSearchResults(for searchController: UISearchController){
}

var filteredTableData = [String]()
var resultSearchController = UISearchController()

private func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}
private func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if self.resultSearchController.isActive {
        return self.filteredTableData.count
    }else{
        return AppDelegate.notes.count
    }
}
private func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    _ = indexPath.section
    _ = indexPath.row
    let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier:"addCategoryCell")
    cell.selectionStyle =  UITableViewCellSelectionStyle.none
    cell.backgroundColor = UIColor.clear
    cell.contentView.backgroundColor = UIColor.clear
    cell.textLabel?.textAlignment = NSTextAlignment.right
    cell.textLabel?.textColor = UIColor.black
    cell.textLabel?.font = UIFont.systemFont(ofSize: 14.0)

    if self.resultSearchController.isActive {
        cell.textLabel?.text = filteredTableData[indexPath.row]
    } else {
        cell.textLabel?.text = filteredTableData[indexPath.row]
    }
    return cell
}

func updateSearchResultsForSearchController(searchController: UISearchController) {
    filteredTableData.removeAll(keepingCapacity: false)
    let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
    let array = (AppDelegate.notes as NSArray).filtered(using: searchPredicate)
    filteredTableData = array as! [String]
    self.tableViewNote.reloadData()

}
    //----------- Searchbar ----------------------

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


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

    let note = AppDelegate.notes[indexPath.row]
    let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath as IndexPath) as! CustomCell
    cell.labelUni.text = note.uniname
    cell.labelPlace.text = note.place
    cell.labelTime.text = note.intime
    cell.labelTime.text = note.outtime
    return cell
}



private func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

}


private func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

}

0 个答案:

没有答案