2015-09-12 44 views
0

我只是簡單地嘗試重新加載TableView與從數組中拉出的新項目和tableView不重新加載新項目。這是代碼。請幫忙嗎?
進口的UIKitTableView不重新載入數據

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

     @IBOutlet weak var myTableView: UITableView! 

     @IBAction func reloadData(sender: UIButton) { 
      tableData = newItems 
      self.tableViewController.tableView.reloadData() 
     } 

     var items = ["AAA", "BBB"] 
     var newItems = ["XXX", "YYY"] 

     var tableData = [String]() 

     var tableViewController = UITableViewController(style: .Plain) 

     override func viewDidLoad() { 
      super.viewDidLoad() 

      tableData = items 
      var tableView = tableViewController.tableView 
      tableView.dataSource = self 
     } 

     override func didReceiveMemoryWarning() { 
      super.didReceiveMemoryWarning() 
     } 


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

     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
      let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)as UITableViewCell 
      cell.textLabel?.text = self.tableData[indexPath.row] 
      return cell 
     } 
    } 
+2

你設置一個委託到你的tableView? 'tableView.delegate = self'? – mikle94

+2

糟糕的設置。你有tableViewController,但tableView本身在你的ViewController中引用,數據源和委託在這裏提供。爲什麼你有tableViewController呢?您在TableViewController的tableView上調用reloadData,而不是在此處引用的myTableView。數據源也設置在tableViewController.tableView,而不是myTableView .. –

+0

嗨mikle90,我在viewDidLoad函數中插入了tableView.delegate = self,但它仍然沒有顯示新的數據 – Simon

回答

1

有在你的代碼中的許多錯誤,這是你的完整的工作代碼:

import UIKit 

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 

    @IBOutlet weak var myTableView: UITableView! 

    var items = ["AAA", "BBB"] 
    var newItems = ["XXX", "YYY"] 

    var tableData = [String]() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     tableData = items 
     myTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell") 

     myTableView.delegate = self 
     myTableView.dataSource = self 


    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ 

     return tableData.count 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ 
     var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell 

     cell.textLabel?.text = tableData[indexPath.row] 

     return cell 
    } 

    @IBAction func reloadData(sender: AnyObject) { 

     tableData = newItems 
     self.myTableView.reloadData() 
    } 
} 

使用此代碼比較你的代碼,你會明白你做了什麼錯。

HERE是更多信息的示例項目。

+0

謝謝Dharmesh。我的代碼中出現語法錯誤:var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier(「cell」)as! UITableViewCell – Simon

+0

你檢查了我的示例項目嗎? –

+0

刪除感嘆號(!)將刪除語法錯誤,但程序崩潰。 – Simon

0

進口的UIKit

類的ViewController:UIViewController中,UITableViewDataSource,{的UITableViewDelegate

@IBOutlet weak var myTableView: UITableView! 
var flag=1 
var items = ["AAA", "BBB"] 
var newItems = ["XXX", "YYY"] 

var tableData = [String]() 

override func viewDidLoad() { 
    super.viewDidLoad() 
    if flag==1{ 
     tableData = items 
    } 
    myTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell") 

    myTableView.delegate = self 
    myTableView.dataSource = self 
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ 

    return tableData.count 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ 
    var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell 

    cell.textLabel?.text = tableData[indexPath.row] 

    return cell 
} 

@IBAction func reloadData(sender: AnyObject) { 
    if flag==1{ 
     tableData = items 
     flag=0 
    }else 
    { 
     tableData=newItems 
     flag=1 
    } 
    self.myTableView.reloadData() 
} 

}

0

import UIKit
class ViewController: UIViewController,UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var myTableView: UITableView! //var flag=1 var items = ["AAA", "BBB"] var newItems = ["XXX", "YYY"] var tableData = String override func viewDidLoad() { super.viewDidLoad() // if flag==1{ // tableData = items // } myTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell") myTableView.delegate = self myTableView.dataSource = self }

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ return tableData.count } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell cell.textLabel?.text = tableData[indexPath.row] return cell } @IBAction func reloadData(sender: AnyObject) { // if flag==1{ // tableData = items // flag=0 // }else // { // tableData=newItems // flag=1 // } self.myTableView.reloadData() }

}

+0

只需在你的代碼中添加註釋行 –

相關問題