2016-08-07 4 views
0

UIViewController内でUITableViewをどのように設定するのですか? UITableViewは動的で、プロトタイプセルを使用します。ViewController内のUITableViewにデータ/行を追加します。 Swift

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

} 

注:storyboardにセットアップDataSourceUITableViewDelegateに忘れてはいけないし(スウィフトを使用)

This is what I have

+0

詳細情報を追加してください。 2つのプロトタイプセルが必要ですか? – MShah

答えて

2

は、次の操作を実行します。 storyboardで既に定義されている場合、プログラムで定義する必要はありません。

+0

ありがとうございます!完璧に働いた!きれいな答え! – Sebbe

+0

ありがとう@Sebbe :) – MShah

0
  1. table viewをView ControllerにCtrl +ドラッグする必要があります。あなたのビューコントローラ内部viewDidLoad()

  2. 、あなたは、デリゲートとデータソースを設定する必要があります。

    tableView.dataSource = self 
    tableView.delegate = self 
    
  3. あなたのビューコントローラを実装する必要があります:

    class YourViewController : UITableViewDelegate, UITableViewDataSource 
    

現在、View Controllerクラスは、セル/行数などを返す関連するメソッドをオーバーライドする必要があります。

幸運。

関連する問題