2016-08-01 13 views
0

私はUIViewControllerをとりました。その後、それにTableViewを持って来ました。そして、テーブルビューのTableViewCellidentifierとそのプロトタイプセルを持つカスタムテーブルビューセルクラスを割り当てました。また、必要な店舗を作成しました。次に、これは親ビューコントローラです:UITableViewはデータを表示していませんか?

class MyViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

    @IBOutlet weak var theTableView: UITableView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view. 

//  let cell = MyTableViewCell() 
//  cell.optionText = UILabel() 
//  cell.optionText?.text = "testing..." 

//  theTableView.addSubview(cell) 
     theTableView.delegate = self 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


    /* 
    // MARK: - Navigation 

    // In a storyboard-based application, you will often want to do a little preparation before navigation 
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
     // Get the new view controller using segue.destinationViewController. 
     // Pass the selected object to the new view controller. 
    } 
    */ 



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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! MyTableViewCell 
     cell.optionText.text = "hello..." 
     return cell 
    } 

} 

私のテーブルは空です。私は何が欠けていますか?

+0

'theTableView.dataSource = self'以外の意味ですか? – NSNoob

+0

ああ私の悪い:) –

答えて

1

はあなたのviewDidLoadに次の行を追加する必要があります:あなたはそれを行うとき

theTableView.dataSource = self 

、あなたのViewControllerから、あなたのテーブルビューにデータソースを提供して、あなたのテーブルビューにデータを示す開始します。

cellForRowAtIndexPathおよびrowsForSectionの両方のメソッドは、データソースメソッドです。あなたのDataSourceを設定していないので、TableViewが提供されたデータを読み込めないのです。

また、Storyboard/Xib経由でTableViewを追加していない場合は、ビューのサブビューとして追加する必要があります。

関連する問題