Ios 在UITableView单元格中加载UITableView的最佳方式是什么?

Ios 在UITableView单元格中加载UITableView的最佳方式是什么?,ios,swift,uitableview,ios8,Ios,Swift,Uitableview,Ios8,我的随意尝试没有调用cellforrowatinexpath。我假设我的设置不对 let wordTableView = WordTableView(frame: CGRectZero) wordTableView.delegate = self wordTableView.dataSource = self wordTableView.words = words let currentCell = wordTableView.cellForRowAtIndexPat

我的随意尝试没有调用
cellforrowatinexpath
。我假设我的设置不对

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。但实际上什么也装不下。如果查看调试视图层次结构,我可以看到没有添加任何内容。因此,我假设我缺少有关实例化此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
数据源并委托给setter类,因此在
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

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
,并将表视图放在其中。

设置
WordTableView
数据源并委托给setter类,因此,在
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

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
inherit
UIViewController
,并将表视图放在其中。

我认为调用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()

我认为调用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()

问题-为什么要将一个表视图放在另一个表视图的单元格中?简单地划分主表视图不是更容易吗?把一个名为
viewDidLoad
的方法放入一个非视图控制器类中真是令人困惑。@rmaddy,谢谢你的提问。我想揭示的是一个动态有序的列表。所以之前,我试着用UILabels和UIViews来实现它,但因为它都是动态的,而且大部分都是编程的,所以我想,对于我的新手来说,它正在变成地狱。。为什么不制作一个xib和一个UITableView来显示一个列表呢。你认为呢?如果我是你,我只会在主表视图中添加更多的行和/或节。嵌套表视图可能会导致滚动和触摸事件出现各种问题。实际上,我接受了您的建议@rmaddy,这就是我的解决方案。使用我现有的tableview。如果您想以某种方式将其作为答案,我会将其标记为正确。问题-为什么要将表视图放在另一个表视图的单元格中?简单地划分主表视图不是更容易吗?把一个名为
viewDidLoad
的方法放入一个非视图控制器类中真是令人困惑。@rmaddy,谢谢你的提问。我想揭示的是一个动态有序的列表。所以之前,我试着用UILabels和UIViews来实现它,但因为它都是动态的,而且大部分都是编程的,所以我想,对于我的新手来说,它正在变成地狱。。为什么不制作一个xib和一个UITableView来显示一个列表呢。你认为呢?如果我是你,我只会在主表视图中添加更多的行和/或节。嵌套表视图可能会导致滚动和触摸事件出现各种问题。实际上,我接受了您的建议@rmaddy,这就是我的解决方案。使用我现有的tableview。如果你想把它变成一个答案,我会把它标记为正确的。