我的偶然嘗試不叫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
}
}
的問題 - 你爲什麼把另一個表視圖的電池內部的表視圖?簡單地區分主表視圖不是更容易嗎?將一個名爲'viewDidLoad'的方法放到非視圖控制器類中真的讓人困惑。 – rmaddy
@rmaddy Yah,謝謝你的提問。這是我試圖揭示的一個動態有序列表。所以之前,我試圖用UILabels和UIViews來做,但是因爲它的所有動態和主要是程序化的,它變成了我的newb自己的地獄,我想......爲什麼不只是製作一個xib,並且一個UITableView來顯示一個名單。你怎麼看? – Trip
如果我是你,我只需添加更多的行和/或部分到主表視圖。嵌套的表格視圖可能會導致滾動和觸摸事件的各種問題。 – rmaddy