Jonathan Meguira
Jonathan Meguira

Reputation: 1969

What is indexPath in NSIndexPath

I have this trunk of code

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

    let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")

    cell.textLabel?.text = toDoList[indexPath.row] //will give us the 0th, 1st, 2nd etc..

    return cell

can somebody explain to me what is the "indexPath" in the first line?

Upvotes: 0

Views: 1837

Answers (3)

Gwendal Roué
Gwendal Roué

Reputation: 4044

indexPath is the local name of the parameter whose external name is cellForRowAtIndexPath.

indexPath is defined inside the function body (and you do use it), while the function caller has to use cellForRowAtIndexPath when they name the function parameter: let cell = tableView.delegate.tableView(tableView, cellForRowAtIndexPath: ...)

Compare:

func sum1(a: Int, b: Int) -> Int {
    return a + b
}

func sum2(a: Int, _ b: Int) -> Int {
    return a + b
}

func sum3(integer a: Int, secondInteger b:Int) -> Int {
    return a + b
}

sum1(1, b: 2)
sum2(1, 2)
sum3(integer: 1, secondInteger: 2)

It is time for you to check Apple documentation about Function Parameter Names: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html

Upvotes: 2

Dimuth
Dimuth

Reputation: 750

NSIndexPath is a sub class of NSObject. It represents the path to specific node in a tree of an array collection.

it has clearly mentioned in here. read this

Your question is based on tableView. you have a collection of cells in a table view. So the node is the cell which you select.

indexPath represents your selected cell in the tree in here.

Upvotes: 0

01axel01christian
01axel01christian

Reputation: 970

Visit https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSIndexPath_Class/

They got a got a good documentation about indexPath.

Upvotes: 0

Related Questions