Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

How to know which methods are required in swift?

I am working with tables in swift. Following one of the tutorials i am told: "There are minimal methods that you need to create a table: numberOfRowsInSection and cellForRowAtIndexPath".

And it happens quite often when a teacher says that some methods are required. But where does this information come from? When i open Apple Documentation i see a lot of methods, but none of them is either highlighted or marked as required.

I guess i am missing something really obvious here...

2 Answers

I don't believe there's a prompt that tells you which methods are required or not. I believe it's just sifting through the docs or in my experience, you could try holding command and clicking the class name of the object you're wanting to implement. There should be a list of properties and methods and all sorts of stuff in the class definition file it takes you to when you do that. Try to search for the keyword "protocol" and see which methods you stumble across. Required methods are the ones that aren't preceded with "optional" before the "func".

protocol UITableViewDataSource : NSObjectProtocol {

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int

    // Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
    // Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

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

    optional func numberOfSectionsInTableView(tableView: UITableView) -> Int // Default is 1 if not implemented

    optional func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? // fixed font style. use custom view (UILabel) if you want something different
    optional func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String?

    // Editing

    // Individual rows can opt out of having the -editing property set for them. If not implemented, all rows are assumed to be editable.
    optional func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool

    // Moving/reordering

    // Allows the reorder accessory view to optionally be shown for a particular row. By default, the reorder control will be shown only if the datasource implements -tableView:moveRowAtIndexPath:toIndexPath:
    optional func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool

    // Index

    optional func sectionIndexTitlesForTableView(tableView: UITableView) -> [AnyObject]! // return list of section titles to display in section index view (e.g. "ABCD...Z#")
    optional func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int // tell table which section corresponds to section title/index (e.g. "B",1))

    // Data manipulation - insert and delete support

    // After a row has the minus or plus button invoked (based on the UITableViewCellEditingStyle for the cell), the dataSource must commit the change
    // Not called for edit actions using UITableViewRowAction - the action's handler will be invoked instead
    optional func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)

    // Data manipulation - reorder / moving support

    optional func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath)
}

Great! This is exactly what i was looking for! Thank you so much!

If you're working with objects that have delegate or data source methods, you'll probably need to implement some methods to give the compiler enough information to make them work right. IE, table views and collection views have delegate and data source methods you have to implement so the compiler knows how many cells/items to produce, what the layout should look like in these objects, and what to do with the cells/items in each row in each section, etc.

https://developer.apple.com/library/prerelease/ios/documentation/UserExperience/Conceptual/TableView_iPhone/CreateConfigureTableView/CreateConfigureTableView.html#//apple_ref/doc/uid/TP40007451-CH6-SW5

Stephen, thank you for your reply.

I clearly understand the concept of the need of implementing some methods.

All i am interested in is if there is any way to know easily and fast which methods are required (means without them i can't use the object).

For instance if i want to create a table, i would open docs on tables and there would be big note saying: "You can create a table only if these two following methods are used: ...."

Of course i am simplifying everything here, but hope it explains what i am trying to find.

My question is not about table in particular. It is about necessary methods in general.

Or there is no such thing as a section/highlight/etc in docs for necessary methods, but i should just research every object with delegate or data source?