vPhong
vPhong

Reputation: 41

Multiple UITableView in a single ViewController with custom UITableViewCell

  1. I implemented 2 TableViews in a single ViewController
  2. One TableView with a custom cell called TreeViewCell. Another with Basic UITableViewCell (both cell draw in the storyboard with its own TableView)

Ask: I have a problem when returning its own cell in cellForRowAtIndexPath

enter image description here

enter image description here

Upvotes: 0

Views: 79

Answers (1)

Crocobag
Crocobag

Reputation: 2340

simply return your cell in your if statement like so :

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    // get rid of the `var cell: UITableViewCell!`

    if tableView == tableViewUser {
        cell = tableViewUser.dequeuReusableCell(withIdentifier: "userCell", for: indexPath) as! TreeViewCell 
        cell.setNodeData(node: self.displayArray[indexPath.row])
        return cell 
    }

    if tableView == tableViewSearch {
        cell = tableViewUser.dequeuReusableCell(withIdentifier: "searchCell", for: indexPath) 
        cell.textLabel.text = "123456"
        return cell 
    }

    return UITableViewCell() // dont worry, you will never go there
}

Also uncomment the register part on your tableViewUser didSet

Upvotes: 1

Related Questions