0

I have a table view that I want to display two different custom cells; however, it is only displaying the first custom cell and nothing else.

class messageThreadViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 2
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        if section == 0 {
            return finalItems.count
        } else {
            return finalItems2.count
        }
    }

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

        if indexPath.section == 0 {           
            let cell = tableView.dequeueReusableCell(withIdentifier: "message1") as! MyTableViewCellThread
            cell.messagelabel?.text = finalItems[indexPath.row]
            return cell
        } else {
            let cell2 = tableView.dequeueReusableCell(withIdentifier: "message2") as! MyTableViewCellThread2
            cell2.messageLabel2?.text = finalItems2[indexPath.row]
            return cell2
       }
    }
}

Again, it's only displaying "cell", not both "cell" and "cell2". I've done a lot of research and couldn't find someone with the same issue. Thanks for your help.

5
  • Are you sure finalItems2 is not empty? Commented Dec 22, 2020 at 8:28
  • replace indexPath.section with indexPath.row in func cellforRowAt, set numberOfRowsInSection : 2 and numberOfSections : 1 Commented Dec 22, 2020 at 9:03
  • furthermore make sure that you've registered both tableviewcells in viewDidLoad() Commented Dec 22, 2020 at 9:13
  • @Deitsch yes it is not empty- I'm able to get either one or the other to show, not both. Commented Dec 22, 2020 at 9:37
  • @DimitrisDelis I apologize because this may not have been clear- numberOfRowsInSection varies based off the contents of "finalItems" and "finalItems2" so I can't set it to 2. Also I am using the interface builder so I have the cells registered on the storyboard. I will update the post avoid confusion. Commented Dec 22, 2020 at 9:42

1 Answer 1

3

method

func numberOfSectionsInTableView(tableView: UITableView) -> Int

is not part of UITableViewDelegate and not called when tableView building ui

the right method to use is

func numberOfSections(in tableView: UITableView) -> Int {
    if section == 0 {
        return finalItems.count
    } else {
        return finalItems2.count
        
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

can you explain a bit more? does this replace "func numberOfSectionsInTableView"? do I have to add "numberOfSections section: Int" to define "section"?
Yes use it to replace numberOfSectionsInTableView
@psettle41 numberOfSectionsInTableView method that you're using is not a part of UITableViewDelegate protocol and not called when table is building
@Kstin thank you for the help, this got it to work!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.