Iphone app source code example
To learn more about the usage of NSArray, you can always refer to Apple’s official document.įinally, we have to add two datasource methods: “tableView:numberOfRowsInSection” and “tableView:cellForRowAtIndexPath”. Later, we’ll invoke the “count” method to query the number of data elements in the array. You can also use other built-in methods to query and manage the array. In our code, we use “arrayWithObjects” to instantiate a NSArray object and preload it with the specific elements (e.g. NSArray offers a set of factory methods to create an array object. If you need a dynamic array, use NSMutableArray instead. You can use NSArray to create static array for which the size is fixed. In Objective C, NSArray is the class for creating and managing array. That means, tableData returns the first element of the “tableData” array.
An array with 10 elements will have indices from 0 to 9. Next, select “SimpleTableViewController.m” and define an instance variable for holding the table data.Įach of the array elements is identified or accessed by an index. We do not change any of these methods in this example. Optional methods of the protocols let you manage the height of a table row, configure section headings and footers, re-order table cells, etc. UITableViewDelegate, on the other hand, deals with the appearance of the UITableView. Through implementing these methods, you tell Table View how many rows to display and the data in each row.
The UITableViewDataSource protocol declares two required methods (tableView:cellForRowAtIndexPath and tableView:numberOfRowsInSection) that you have to implement. It’s the link between your data and the table view. So how do you tell UITableView the list of data to display? UITableViewDataSource is the answer. Or like this example, we’ll use the table view to present a list of recipes. You may display a list of countries or contact names. The UITableView, the actual class behind the Table View, is designed to be flexible to handle various types of data. UITableViewDelegate and UITableViewDataSourceĮarlier, we’ve added the “UITableViewDelegate” and “UITableViewDataSource” protocols in the header file. For now, you do not need the unit test class. Include Unit Tests: – Leave this box unchecked.Use Automatic Reference Counting: – By default, this should be enabled.We do not need Storyboards for this simple project. Use Storyboards: – Do not select this option.Device Family: iPhone – Just use “iPhone” for this project.But for this tutorial, let’s keep it simple and use “SimpleTable”. In future, you may choose your own prefix or even leave it blank. Class Prefix: SimpleTable – Xcode uses the class prefix to name the class automatically.Otherwise, you may use mine or just fill in “edu.self”. If you have a domain, you can use your own domain name. Company Identifier: com.appcoda – It’s actually the domain name written the other way round.Product Name: SimpleTable – This is the name of your app.Again, fill in all the required options for the Xcode project: