0

Here is the code and here is my resulting display. I'm using a table view object, not a table view controller (table view controller works fine), because I intend to do something with the background. Idk why the sample text isn't displaying? This is probably really stupid... Here is the image of the output: !https://drive.google.com/file/d/1tEWZUjWP-8FhZKDvuAQyVV2vkh2_dyRL/view?usp=sharing And here is the image of how I configured the cells (I have another split view controller for another purpose, but never mind that since it's unrelated to problem): !https://drive.google.com/file/d/1u-T1J6xp1bZq12HRCSeD3Eq_VWlGH4lb/view?usp=sharing

Please help and thanks! I also tried to look at How to insert new cell into UITableView in Swift and used the +21 response

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    //MARK: Properties
    @IBOutlet weak var drawingCanvas: UIView!
    @IBOutlet weak var tableView: UITableView!
    var tableData = ["one"]

    //MARK: Table View
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return tableData.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if tableView == self.tableView {
            let cell = tableView.dequeueReusableCell(withIdentifier: "TaskCell", for: indexPath)
            // Configure the cell...
            cell.textLabel?.text = tableData[indexPath.row]
            return cell
        }
        return UITableViewCell()
    }


}
vadian
  • 274,689
  • 30
  • 353
  • 361
klee
  • 217
  • 1
  • 4
  • 11
  • Do you provide Datasource and Delegate to your tableview object? – Kazi Abdullah Al Mamun Jul 28 '19 at 06:19
  • Is the `datasource` of the tableview connected to the class in Interface Builder? It must be. And the check `if tableView == self.tableView` is pointless if there is only one table view. And the signature of `numberOfSections` is wrong. – vadian Jul 28 '19 at 06:20
  • Oh okay, got it; wrote tableView.delegate = self, tableView.dataSource = self in viewDidLoad() and it worked. Was this the proper fix? – klee Jul 28 '19 at 06:24
  • @vadian: Oh okay, got it; wrote tableView.delegate = self, tableView.dataSource = self in viewDidLoad() and it worked. Was this the proper fix? – klee Jul 28 '19 at 06:38
  • Yes, but I'd prefer to connect it in Interface Builder rather than in code. – vadian Jul 28 '19 at 06:43
  • @vadian Can you clarify what you mean by that? – klee Jul 28 '19 at 19:52
  • In the project select `Main.storyboard` or whatever the name of your UI object is, then select the table view (not the enclosing scroll view). Press and hold the control key and drag from the table view to the blue object at the top of the scene. You will see `dataSource` and `delegate` in the `Outlets` section. Select `dataSource`. Do it once more and select `delegate` although your code doesn't require `UITableViewDelegate` – vadian Jul 28 '19 at 20:03
  • @vadian Oh I see, thanks for the clarification! – klee Jul 29 '19 at 23:55

1 Answers1

2

Your tableview can't show data because you haven't been setting datasource and delegate for ViewController to display data.

You should register delegate and datasource of tableview in viewDidload() Please follow the bellow code:

self.tableView.delegate = self self.tableView.dataSource = self