reloadData()不起作用

时间:2016-06-21 18:18:40

标签: ios swift uitableview

我到处都看过教程,没有一个人帮助过我...... reloadData绝对没有什么

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.
    }


}

1 个答案:

答案 0 :(得分:0)

在viewDidLoad上添加此内容

self.table.delegate = self self.table.dataSource = self