ios - 在 UITableView 单元格中加载 UITableView 的最佳方法是什么?

标签 ios swift uitableview ios8

我的偶然尝试没有调用 cellForRowAtIndexPath 。我假设我的设置不正确。

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() 被调用。但实际上没有加载。如果查看 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
    }
}

最佳答案

您将wordTableView数据源和delegate设置给了setter类,所以table view数据源和delegate函数不会在WordTableView类中被调用。
既然你说你的 viewDidLoad 被调用,让我们在这个函数中设置委托(delegate)。

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 并将您的里面的表格 View 。

关于ios - 在 UITableView 单元格中加载 UITableView 的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36518182/

相关文章:

ios - 即使在后台线程上,将 UIImage 写入文件也会锁定 UI

ios - 是否可以使用 Storyboard创建非全屏 UITableView?

swift - 在 Swift 中向每一行添加值,但在行出队和重用时不添加值

swift - 如何在 Swift 3 中向图像添加文本?

ios - 在 Swift 3 的 UserDefaults 中记录 UISwitch 状态的最佳方法是什么

ios - 有人可以为我提供 "refreshObject:mergeChanges:YES"的说明吗?

ios - 为整个应用更改 UITableViewCell 的背景 View

我无法弄清楚的iOS7状态栏问题

ios - 每次回到欧盟都会弹出 Admob GDPR 同意对话框?

iphone - UITextView行高问题