Ios 重载数据()不';行不通

Ios 重载数据()不';行不通,ios,swift,uitableview,Ios,Swift,Uitableview,我到处都看过教程,没有一本对我有帮助。。。。重新加载数据完全不起任何作用 import UIKit class searchView: UIViewController, UITableViewDelegate, UITableViewDataSource{ @IBOutlet var table: UITableView! var user: User! var updatedSearch: String = "" var keys: [String]!

我到处都看过教程,没有一本对我有帮助。。。。重新加载数据完全不起任何作用

import UIKit

class searchView: UIViewController, UITableViewDelegate, UITableViewDataSource{

    @IBOutlet var table: UITableView!
    var user: User!
    var updatedSearch: String = ""
    var keys: [String]!
    var dict: [String: [String]]!
    var tableArray: [String] = ["15", "14", "13", "12", "11", "10", "9", "8", "7", "6", "5", "4", "3", "2", "1"]
    let size = 15

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        print("Section: \(section)")
        return tableArray.count
    }

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

        let cell:UITableViewCell = self.table.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell

        cell.textLabel?.text = self.tableArray[indexPath.row]
        print("Cell: \(cell.textLabel!.text!)")
        return cell
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print("You selected cell #\(indexPath.row)!")
    }

    func textFieldDidChange(textField: UITextField) {
        print("Text Changed")
        updateTable()
    }
    @IBOutlet weak var searchBar: UITextField!

    @IBAction func doneNavButton(sender: UIButton) {
        navigationController?.popViewControllerAnimated(true)
    }

    func readCityDocument() -> NSDictionary{
        let path = NSBundle.mainBundle().pathForResource("cities", ofType: "txt")
        do{
            let content = try String(contentsOfFile: path!, encoding: NSUTF8StringEncoding)
            return getDictionary(content)
        }
        catch{
            print("error")
        }
        return NSDictionary()
    }

    func getDictionary(doc: String) -> NSDictionary{
        print("Parsing Document....")
        let document = doc.componentsSeparatedByString("\n")
        var doc2: [[String]] = []
        var dict: [String: [String]] = NSDictionary() as! [String : [String]]
        for x in document{
            doc2.append(x.componentsSeparatedByString(","))
        }
        for y in doc2{
            dict[y[0]] = y
        }
        print("Done....")
        return dict
    }

    func updateTable(){
        var siz = size
        var newTableArray = [String]()
        for x in keys{
            if x.hasPrefix(searchBar.text!){
                let val: String!
                if (dict[x]!.count == 4){
                    let ar = dict[x]
                    val = "\(ar![0]), \(ar![2]), \(ar![3])"
                }
                else{
                    let ar = dict[x]
                    val = "\(ar![0]), \(ar![1]), \(ar![2])"
                }
                newTableArray.append(val)
                siz = siz - 1
                if (siz == 0){
                    break
                }
            }
        }
        self.tableArray = newTableArray
        print("reload the damn Data...")
        self.table.reloadData()

    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    }

    override func viewDidLoad() {
        super.viewDidLoad()
        var x = size
        while (x > 0){
            tableArray.append("\(x)")
            x = x - 1
        }
        print(tableArray.description)
        self.table.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
        print("Creating Dictionary from City Document...")
        dict = readCityDocument() as! [String : [String]]
        keys = []
        for (key, _) in dict{
            keys.append(key)
        }
        searchBar.addTarget(self, action: #selector(searchView.textFieldDidChange(_:)), forControlEvents: UIControlEvents.EditingChanged)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        print("Memory Warning?")
        // Dispose of any resources that can be recreated.
    }


}

将此添加到viewDidLoad

self.table.delegate=self

self.table.dataSource=self

欢迎来到stackoverflow!请澄清您的具体问题或添加其他详细信息,以突出显示您所需的内容。正如目前所写的,很难准确地说出你在问什么。请参阅帮助澄清此问题的页面。好的,这解决了我所有的问题,尽管我已经在主故事板上完成了这项工作。。。我最终删除了这个表,并放入了一个新表,结果它成功了。我一定是弄乱了旧表上的设置,导致代码中出现问题。谢谢,哈哈!