带有UISearchController的resultController中的空白结果

时间:2016-06-24 14:40:19

标签: swift2 uisearchcontroller

我使用2个tableViewController,每个都有自定义原型(一个用于搜索,一个用于结果)

我在控制台(打印...)中有好结果,但我的自定义单元格是空白的

这是我的代码,如果有人可以提供帮助

import UIKit

class search_TableViewController: UITableViewController, UISearchResultsUpdating {

    @IBOutlet var sTableView: UITableView!

    // Properties
    // 1
    var users:[[String:AnyObject]]!
    var foundUsers:[[String:AnyObject]]!

    // 2
    var userDetails:[String:AnyObject]!

    // 3
    var resultController: UITableViewController!
    var searchController: UISearchController!



    override func viewDidLoad() {
        super.viewDidLoad()

        users = []
        foundUsers = []
        self.sTableView.dataSource = self
        self.sTableView.delegate = self
        self.automaticallyAdjustsScrollViewInsets = false



        //ViewDidLoad
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        resultController = storyboard.instantiateViewControllerWithIdentifier("resultId") as! UITableViewController
        resultController.tableView.delegate = self;
        resultController.tableView.dataSource = self
        resultController.tableView.delegate = self;
        resultController.tableView.registerClass(result_TableViewCell.self, forCellReuseIdentifier: "resultPRTC")



        // 3
        searchController = UISearchController(searchResultsController: resultController)
        searchController.hidesNavigationBarDuringPresentation = true
        searchController.searchBar.searchBarStyle = .Minimal
        searchController.searchResultsUpdater = self
        self.sTableView.tableHeaderView = searchController.searchBar


        // 4
        self.definesPresentationContext = true
        // 5
        let session = NSURLSession.sharedSession()
        let url:NSURL! = NSURL(string: "http://jsonplaceholder.typicode.com/users")
        let task = session.downloadTaskWithURL(url) { (location: NSURL?, response: NSURLResponse?, error: NSError?) -> Void in
            if (location != nil){
                let data:NSData! = NSData(contentsOfURL: location!)
                do{
                    self.users = try NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves) as! [[String : AnyObject]]
                    dispatch_async(dispatch_get_main_queue(), { () -> Void in
                        self.sTableView.reloadData()
                    })
                }catch{
                    // Catch any exception
                    print("Something went wrong")
                }
            }else{
                // Error
                print("An error occurred \(error)")
            }
        }
        // Start the download task
        task.resume()
    }


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

    //MARK: UISearchResultsUpdating
    func updateSearchResultsForSearchController(searchController: UISearchController) {
        foundUsers.removeAll()
        for user in users{
            let userName:String! = user["name"] as? String
            if userName.localizedCaseInsensitiveContainsString(searchController.searchBar.text!) {
                foundUsers.append(user)
                 print ("Liste 1 : \(foundUsers)")
                self.resultController.tableView.reloadData()
            }
        }
    }



    //MARK: UITableViewDataSource
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //print (tableView)


        if tableView == self.sTableView{
            print ("userSearch :\(users.count)")
            return users.count

        }

        //passe sur la recherche
        print ("userResult:\(foundUsers.count)")
        return foundUsers.count


    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        var cellIdentifier: String!
        var dic: [String:AnyObject]!

        if tableView == self.sTableView{
            cellIdentifier = "searchPRTC"
            dic = self.users[indexPath.row]
            let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! search_TableViewCell
            cell.sNom?.text = dic["name"] as? String
            cell.sEmail?.text = dic["email"] as? String
            return cell

        }else{
            cellIdentifier = "resultPRTC"
            dic = self.foundUsers[indexPath.row]
            let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! result_TableViewCell
            cell.rNom?.text = dic["name"] as? String

              print ("Liste 2 : \(foundUsers)")
            return cell

        }

        // let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)
        //cell.textLabel?.text = dic["name"] as? String
        //   cell.adresse?.text = dic["username"] as? String

    }




}

如果我使用结果这个代码而不是我的自定义原型我看到结果 - 也许这些信息可以帮助很多:

 let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)
            cell.textLabel?.text = dic["name"] as? String

1 个答案:

答案 0 :(得分:0)

我拥有它:  必须删除这个!

resultController.tableView.registerClass(result_TableViewCell.self, forCellReuseIdentifier: "resultPRTC")

有人知道是否可以只有一个视图?或者我必须有2个tableview控制器(一个用于搜索结果)

TY