Parth Patel
Parth Patel

Reputation: 155

UITableView Displaying Incorrectly

I am trying to use a UITableView by implementing the code I saw on many tutorials but it doesn't work correctly when I run it. What am I doing wrong? I added a cell to the view and added a corresponding identifier. My storyboard look like that and code is the following:

import UIKit
import Firebase
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    var ref: DatabaseReference?
    var data = ["m","2","3"]
    @IBOutlet weak var tableV: UITableView!
    
    override func viewDidLoad() {
    
        super.viewDidLoad()
        tableV.delegate = self
        tableV.dataSource = self
        // Do any additional setup after loading the view, typically from a nib.

    }

    override func didReceiveMemoryWarning() {
        
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
        let cell = tableV.dequeueReusableCell(withIdentifier: "PostCell")
        cell?.textLabel?.text = data[indexPath.row]
        return cell!
    }
}

And what it looks like. Please help me to solve this problem.

Upvotes: 1

Views: 79

Answers (1)

Ataberk Kocaman
Ataberk Kocaman

Reputation: 69

You should use this way to correct errors.

change this:

let cell = tableV.dequeueReusableCell(withIdentifier: "PostCell")

with:

let cell = tableV.dequeueReusableCell(withIdentifier: "PostCell", for: indexPath)

and you need to check cell identifier.

Upvotes: 1

Related Questions