除非UITableViewAutomaticDimension对包含该表的单元格不起作用,否则我可以正常工作。我能让它工作的唯一方法是通过在tableView中硬编码高度(tableView:UITableView,heightForRowAtIndexPath indexPath:NSIndexPath)
let data: [AnyObject] = ["one\ntwo\nthree", "cat\ndog\nhorse", ["apple", "bannana", "carrot"]]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableView.registerNib(UINib(nibName: "tableCell", bundle: nil), forCellReuseIdentifier: "table")
tableView.registerNib(UINib(nibName: "textCell", bundle: nil), forCellReuseIdentifier: "text")
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 50
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if let data = data[indexPath.row] as? [AnyObject] {
let cell: TableCell = tableView.dequeueReusableCellWithIdentifier("table")! as! TableCell
cell.data = data
cell.backgroundColor = UIColor.orangeColor()
cell.childTableView.dataSource = cell
return cell
} else {
let cell: TextCell = tableView.dequeueReusableCellWithIdentifier("text")! as! TextCell
cell.textView.text = "\(data[indexPath.row])"
cell.backgroundColor = UIColor.lightGrayColor()
return cell
}
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if let data = data[indexPath.row] as? [AnyObject] {
//return 200 //need to uncommon this to make it work
}
return UITableViewAutomaticDimension
}
}
请参阅完整代码
https://github.com/zackhalil/swift-iOS-table-inside-table-cell.git