Kevin ABRIOUX
Kevin ABRIOUX

Reputation: 17695

numberOfSectionsInTableView not called

In my ViewController, i have an UITableView and these methods :

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if(self.mQuoteObjects.count > 0){
        return 1
    }
    return 0

}


func tableView(tableView: UITableView, numberOfSectionsInTableView section: Int) -> Int {
    let nbItem = self.mQuoteObjects.count
    return nbItem

}

Method "numberOfRowsInSection" is correctly called, but "numberOfSectionsInTableView" is never called.

What have i missed?

You can respond in Obj-c

Upvotes: 6

Views: 6554

Answers (5)

Chetu Test
Chetu Test

Reputation: 113

If any one is Using SWIFT 5

 func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 2
    }

Upvotes: 7

gugapc
gugapc

Reputation: 224

There is a bug in Swift 5 about inheritance, for example:

If you have a class that implements UITableViewDataSource but, does not implement numberOfSections, only in the subclass, this function will never be called. You need to declare this function in this class and override in the subclass.

For more information about: https://forums.developer.apple.com/thread/115579 https://bugs.swift.org/browse/SR-10257

Upvotes: 2

Siddharth Chauhan
Siddharth Chauhan

Reputation: 1327

For Swift 3

func numberOfSections(in tableView: UITableView) -> Int {

}

Upvotes: 3

Robert
Robert

Reputation: 3870

The method name is wrong

try this:

 func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return self.mQuoteObjects.count
 }

Upvotes: 0

Marcos Crispino
Marcos Crispino

Reputation: 8218

The name of the method is not correct. It should be numberOfSectionsInTableView(_:).

See the UITableViewDataSource protocol reference.

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return self.mQuoteObjects.count
}

Upvotes: 11

Related Questions