0

I have it working except the UITableViewAutomaticDimension does not work on the cell that has the table. The only way I can get it to work is by hard coding the height in the 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
}
}

See Full code

https://github.com/zackhalil/swift-iOS-table-inside-table-cell.git

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
zackhalil
  • 455
  • 3
  • 14
  • 2
    I don't have a solution to your specific problem but what you describe sounds like a bad UI in the making. If you scroll the inner-most table view, what's supposed to happen? Or is the inner table view static and non-scrolling? – Duncan C Dec 09 '15 at 01:53
  • 1
    @DuncanC i disabled vertical scroll in the inner table. I want the entire table to be displayed... I had similar problem in android version of same app... Was able to solve it by placing a NonScrollableListView inside the ListView so only the outer list is scrollable – zackhalil Dec 09 '15 at 03:27

0 Answers0