ray
ray

Reputation: 933

Swift cellForRowAtIndexPath definition conflicts with previous value

Below is the code for my tableView, I have an error on the cellForRowAtIndexPath function declaration that says definition conflicts with previous value. A previous stack overflow question has the solution to make the function return UITableViewCell as an optional value but this does not fix the error for me. I also have an error that says viewController does not conform to protocol UITableViewDataSource, but I assume this is because of the error on the cellForRowAtIndexPath method.

I have similar code for a tableView that works, but it was written about a month ago before I updated Xcode. Maybe a recent change in swift is causing the error?

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

        return 1

    }


    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell? {

        let cellIdentifier = "tableViewCell"

        let newCell : TableViewCell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier)  as! TableViewCell


        newCell.heading.text = names[indexPath.row]

        return newCell


    }

Upvotes: 2

Views: 833

Answers (2)

GetSwifty
GetSwifty

Reputation: 7746

If this is in a UITableViewController, you need to add override to the method:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

Upvotes: 0

Alex Bollbach
Alex Bollbach

Reputation: 4570

Try placing your datasource methods in a class extension like this:

extension yourViewController: UITableViewDataSource {
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { // cellForRow stuff 
    }

}

Upvotes: 2

Related Questions