2016-08-07 17 views

回答

2

請執行以下操作:

import UIKit 

class tableViewOne: UIViewController{ 

    @IBOutlet var tableView: UITableView! 
    // your data 
    var dummyData = ["data 0","data 1","data 2"] 

    override func viewDidLoad() { 
     super.viewDidLoad() 

    } 

} 

extension tableViewOne: UITableViewDataSource, UITableViewDelegate { 
    // Define no of rows in your tableView 
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return dummyData.count 
    } 

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

     cell.textLabel!.text = dummyData[indexPath.row] 

     return cell; 
    } 

} 

注:不要忘記設置DataSourceUITableViewDelegatestoryboard。如果已經在storyboard中定義,則無需以編程方式定義。

+0

謝謝!完美工作!乾淨的回答! – Sebbe

+0

Thanks @Sebbe :) – MShah

0
  1. 您需要按Ctrl +拖動您的table view到您的視圖控制器。

  2. 在您的視圖控制器內viewDidLoad(),您將需要設置委託和數據源:

    tableView.dataSource = self 
    tableView.delegate = self 
    
  3. 您的視圖控制器需要實現:

    class YourViewController : UITableViewDelegate, UITableViewDataSource 
    

現在,您的View Controller類將需要覆蓋返回單元格/行數等的相關方法。

祝你好運。

相關問題