2016-06-21 70 views
-1

我在教程到处找,而不是单一的一个帮助我.... reloadData绝对没有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. 
    } 


} 
+0

欢迎来到stackoverflow!请澄清您的具体问题或添加更多的细节,以确切地突出你所需要的。正如目前所写,很难确切地说出你在问什么。请参阅[如何提问](https://stackoverflow.com/help/how-to-ask)页面以获得澄清此问题的帮助。 – Phonolog

回答

0

添加这对viewDidLoad中

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

+0

好吧,这解决了我所有的问题,即使我已经从主故事板上完成了这个任务......我最终删除了桌子,并且放入了一个新桌子并且工作。我一定弄错了旧表上的设置,并导致它在代码中出现问题。谢谢,虽然哈哈! –

相关问题