2016-04-09 130 views
0

我的偶然嘗試不叫cellForRowAtIndexPath。我假設我的設置不正確。在UITableView單元內加載UITableView的最佳方式是什麼?

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() 

當我運行這個,我的UITableView的viewDidLoad()get的調用。但沒有實際加載。如果我查看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和UIViews來做,但是因爲它的所有動態和主要是程序化的,它變成了我的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() 
相關問題