2017-03-11 9 views
1

私はtableViewtableViewCellをストーリーボードを使用せずにプログラム的に設計しました。 ViewControllerでの私のviewDidLoad()はこのようなものになります。tableView DelegateメソッドとDataSourceメソッドを呼び出せません

tableView.delegate = self 
tableView.dataSource = self 
tableView.register(TicketsTableViewCell.self,forCellReuseIdentifier:cellReuseIdentifier) 
tableView = UITableView(frame: UIScreen.main.bounds, style: .plain) 
self.view.addSubview(tableView) 

をそして、私のtableViewCellは次のようになります。

class TicketsTableViewCell: UITableViewCell { 

override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
    super.init(style: style, reuseIdentifier: reuseIdentifier) 

    //Other View related stuff 
} 

required init?(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 
} 
required init?(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 
} 

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

、私はそれを実行したとき、私はtableViewを見ることができるよさのもの、細胞は含まない。さらに、cellForRowAt:にブレークポイントを追加すると、呼び出されません。私は間違っているのですか?再利用識別子で何か問題がありますか? ありがとうございます。

+0

numberOfSectionsInTableViewまたは他の代理人メソッド呼び出しをチェックすることはできますか? - http://stackoverflow.com/questions/24023613/programmatically-uitableview-using-swift –

+0

データソースとは何ですか?いくつの要素がありますか?テーブルにはこれらの情報が必要です。 –

+0

データソースは辞書であり、 'numberOfRowsInSection'が呼び出されなくても。 @ElTomato –

答えて

2

あなたのためになるようにラインが最初に入れ役立つかもしれないようにしてみdelegateを設定datasourceとレジスタセル。

tableView = UITableView(frame: UIScreen.main.bounds, style: .plain) 
tableView.delegate = self 
tableView.dataSource = self 
tableView.register(TicketsTableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier) 
self.view.addSubview(tableView) 
+0

ありがとうございます。これはうまくいった。私が作ったような小さな間違いを信じられませんでした。 –

+0

@ shravan.sukumarようこそメイト:) –

0

これは問題が最初にあなたが行tableView = UITableView(frame: UIScreen.main.bounds, style: .plain)tableViewを再初期化され、その後tableViewdelegatedatasourceを設定しているされて

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource 
{ 

var tableView : UITableView! 


override func viewDidLoad() { 
    super.viewDidLoad() 

    self.view.backgroundColor = UIColor.whiteColor() 
    self.setUpTableView() 

} 
func setUpTableView() 
{ 
    // Create only one table view. 
    tableView = UITableView(frame: CGRectMake(self.view.frame.size.width/10, self.view.frame.size.height/2, self.view.frame.size.width - self.view.frame.size.width/5, self.view.frame.size.height/2 - 20), style: UITableViewStyle.Grouped) 
    tableView.dataSource = self 
    tableView.delegate = self 
    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell") 
    tableView.layer.cornerRadius = 10 
    tableView.layer.borderColor = UIColor.blackColor().CGColor 
    tableView.layer.borderWidth = 2 
    self.view.addSubview(tableView) 
} 
//table view data source 

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

    return 3 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell 

    cell.textLabel?.text = "test" 
    cell.textLabel?.numberOfLines = 0 

    return cell 

    } 

} 
関連する問題