Krish Solanki
Krish Solanki

Reputation: 269

What is the use of tableview's delegate method cellforrowatindexpath in swift?

i am fresher and i am doing my app in swift language and i want to know that what is the difference between

func cellForRowAtIndexPath(indexPath: NSIndexPath) -> UITableViewCell?

and

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

delegate methods in UITableView?

Upvotes: 0

Views: 573

Answers (3)

Ilesh P
Ilesh P

Reputation: 4046

The first method

func cellForRowAtIndexPath(indexPath: NSIndexPath) -> UITableViewCell?

It is an instance method in UITableView Class. And it is used to get a cell from tableView instance by passing indexPath as parameter .

and the second one

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

is the datasource method of UITableView and it is one of the require method in UITableView.

Thank you.

Upvotes: 1

Akhilrajtr
Akhilrajtr

Reputation: 5182

As a one line answer to the question,

The first method

func cellForRowAtIndexPath(indexPath: NSIndexPath) -> UITableViewCell?

is not tableView delegate method. It an instance method in UITableView Class. Which is used to get a cell from tableView instance by passing indexPath as parameter. So you will be using this method on a tableView instance.

The second method

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

is the datasource method of UITableView that is used to populate tableview cells. This method will be implemented in the datasource/delegate class.

Upvotes: 2

streem
streem

Reputation: 9144

func cellForRowAtIndexPath(indexPath: NSIndexPath) -> UITableViewCell? is a UITableView method, it is used to get a cell at a given time when the tableview is defined. For instance, it can be used to check is a cell is visible. If it is not visible this method will return nil.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell is a UITableViewDataSource method, it is used to define all the cells of the tableview, so that it can know what to display.

Upvotes: 1

Related Questions