user2999910
user2999910

Reputation:

Get current section of UITableView with Swift

I cannot get a Swift section. I used the code:

var section = tableView.visibleCells (). first? .section

But for some reason, it returns nil.

Upvotes: 4

Views: 13914

Answers (3)

StudentX
StudentX

Reputation: 2496

You could try this (Swift 3):

var section = 0
if let index = tableView.indexPathsForVisibleRows?.first?.section {
   section = index
}

Upvotes: 2

Nagarjun
Nagarjun

Reputation: 6867

Here is solution write this code

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell? {
NSLog("Index Path Section %d", indexPath.section)
}

Upvotes: 2

Ashish Kakkad
Ashish Kakkad

Reputation: 23882

If you want to get current section from the delegate methods of UITableView then use following :

var sectionNumber = indexPath.section

else you can use :

let indexPath = tableView.indexPathForSelectedRow();
var sectionNumber = indexPath.section

Upvotes: 10

Related Questions