2016-04-09 2 views
0

わたしの無謀な試みはcellForRowAtIndexPathを呼んでいません。セットアップが正しくないと思っています。UITableViewの内部でUITableViewを読み込む最良の方法は何ですか?

私はこれを実行すると、私のUITableViewののviewDidLoadは()のが呼び出される
let wordTableView  = WordTableView(frame: CGRectZero) 
wordTableView.delegate = self 
wordTableView.dataSource = self 
wordTableView.words  = words 
let currentCell = wordTableView.cellForRowAtIndexPath(indexPath) 
cell.contentView.addSubview(wordTableView) 
wordTableView.viewDidLoad() 
wordTableView.reloadData() 

。しかし実際には何もロードされません。私がDebug View Hierarchyを見ると、何も追加されていないことがわかります。だから私は、このtableViewのインスタンス化について特に何かが欠けていると仮定します。

参考までに、これは私のUITableViewです。しかし、正直言って、私は完全には、UITableViewがインスタンス化する必要があるものを完全に推測しています。そして、これは私の最高の試みのようになります。

class WordTableView: UITableView { 

    var words = [Word]() 

    func viewDidLoad() { 
     self.registerNib(UINib(nibName: "WordCell", bundle: nil), forCellReuseIdentifier: "wordCell") 
    } 

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

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

     let word = words[indexPath.row] 
     cell.sanskrit.text = "Sanskrit" //word.valueForKey("sanskrit") 
     cell.definition.text = "Definition" // word.valueForKey("definition") 

     return cell 
    } 
} 
+0

質問 - なぜあなたは別のテーブルビューのセル内のテーブルビューを入れていますか?メインテーブルビューを単に分割する方が簡単ではないでしょうか?そして、 'viewDidLoad'というメソッドを非ビューコントローラクラスに入れるのは本当に混乱します。 – rmaddy

+0

@rmaddy Yah、尋ねてくれてありがとう。私が明らかにしようとしている動的な順序付けされたリストです。以前は、私はUILabelsとUIViewでそれをやろうとしていましたが、そのすべてのダイナミックで主にプログラム的なので、私のnewb自身のために地獄になっていました。なぜなら、xibとUITableViewをリスト。どう思いますか? – Trip

+1

私があなただったら、メインテーブルビューに行やセクションを追加するだけです。ネストした表のビューは、スクロールおよびタッチ・イベントであらゆる種類の問題を引き起こす可能性があります。 – rmaddy

答えて

1

あなたはセッタークラスにwordTableViewデータソースとデリゲートを設定し、そのテーブルビューのデータソースとデリゲート関数がWordTableViewクラスで呼び出されることはありません。
viewDidLoadが呼び出されたので、この関数内にデリゲートを設定してください。

let wordTableView = WordTableView(frame: CGRectZero) 
wordTableView.words = words 
let currentCell = wordTableView.cellForRowAtIndexPath(indexPath) 
cell.contentView.addSubview(wordTableView) 
wordTableView.viewDidLoad() 
wordTableView.reloadData() 

とサイドノートとしてWordTableViewクラス

class WordTableView: UITableView, UITableViewDelegate, UITableViewDataSource { 

    var words = [Word]() 

    func viewDidLoad() { 
     self.registerNib(UINib(nibName: "WordCell", bundle: nil), forCellReuseIdentifier: "wordCell") 
     self.delegate = self 
     self.dataSource = self 
    } 

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

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

     let word = words[indexPath.row] 
     cell.sanskrit.text = "Sanskrit" //word.valueForKey("sanskrit") 
     cell.definition.text = "Definition" // word.valueForKey("definition") 

     return cell 
    } 
} 

、あなたは直接viewDidLoad()を呼び出すべきではありません、あなたはWordTableView継承UIViewControllerを作り、その中にあなたのテーブルビューを置けば、それは良いです。

1

明示的に行っているので、あなたのviewDidLoadが呼び出されていると思いますが、フレームはCGRectZeroなので、技術的にはサイズがないので、何もサブビューとして追加することはないので何も呼び出されません。

テーブルビューを初期化するより良い方法は、独自のイニシャライザを作成することです。

class WordTableView: UITableView { 

var words = [Word]() 

init(frame: CGRect, wordArray: [Word], delegate: UITableViewDelegate, dataSource: UITableViewDataSource) { 
    self.frame = frame 
    words = wordArray 
    self.delegate = delegate 
    self.dataSource = dataSource 
    self.reloadData() 
} 

func viewDidLoad() { 
    self.registerNib(UINib(nibName: "WordCell", bundle: nil), forCellReuseIdentifier: "wordCell") 
} 

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

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

    let word = words[indexPath.row] 
    cell.sanskrit.text = "Sanskrit" //word.valueForKey("sanskrit") 
    cell.definition.text = "Definition" // word.valueForKey("definition") 

    return cell 
} 

}

そしてセットアップ中:

let wordTableView = WordTableView(frame: cell.contentView.bounds, wordArray: words, delegate: self, dataSource: self) 
let currentCell = wordTableView.cellForRowAtIndexPath(indexPath) 
cell.contentView.addSubview(wordTableView) 
cell.setNeedsDisplay()