MattBlack
MattBlack

Reputation: 3828

Table View cellForRowAtIndexPath warning

I have updated my code to swift 3.0 and get a warning on the following line:

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

When I try each of the suggestions to either silence the warning with @nonobjc or make it a private function the table no longer loads.

The error reads:

Instance method 'tableView(:cellForRowAtIndexPath:)' nearly matches optional requirement 'tableView(:canFocusRowAt:)' of protocol 'UITableViewDelegate'

Does anyone know what causes this error and how to fix it?

Many thanks!

Upvotes: 16

Views: 6322

Answers (3)

mrtnha
mrtnha

Reputation: 1

I had a similar problem, and found that if you remove the space between the underscore before tableView, from this

func tableView(_ tableView: ...

to this

func tableView(_tableView: ...

strangely the warning goes away...

Upvotes: -1

Steve Alston
Steve Alston

Reputation: 236

Just add the declaration of implementing UITableViewDataSource protocol to the class definition like this:

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {}

Upvotes: 22

MAB
MAB

Reputation: 963

In swift 3.0 the signature for the datasource changed to:

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

Notice the difference between cellForRowAtIndexPath indexPath: IndexPath and cellForRowAt indexPath: IndexPath

I'm using the new method without any warnings, hope this will solve your problem.

Cheers.

Upvotes: 6

Related Questions