In this article I will present a sample application that uses UITableView and SQLite to display data stored in a SQLite database. This application will log and display in a UITableView blood pressure samples created by the user. It will store the date and time, the systolic and diastolic values and the pulses per minutes. This is a port of the application created in this article. The following images show the data collection and the main tableView screens
Lets start by creating a new iphone navigation based project. Click file >>new >>Solution and select navigation based project and name it iBloodPressure. First lets create a class definition that will hold the data to be stored in the SQLite database. Click File>>New>>File and select General>>Empty class template. Add the following code.
As you can see this class definition describes the data that will be stored in the data base. Now, to create and access the SQLite database, I am going to use sqlite-net (http://code.google.com/p/sqlite-net/) which is an open source, minimal library to allow .NET and Mono applications to store data in [http://www.sqlite.org SQLite 3 databases]. The great thing about this library is that you can use Linq to query the database. Now, double click RootViewController.xib.cs and add the following code.
On line 29 I add a UIBarButtonSystemItem.Add button to the navigation bar. When this button is pressed it will invoke the insertObject selector method. A Selector object is a cocoa touch equivalent of an Action<T> delegate in c#. Therefore I create a Selector object by passing a string representing the name of the method that is to be invoked. After the UIBarButtonItem is created it is assigned to the NavigationItem.RightBarButtonItem. On line 35 I create a connection to the database, if the database does not exist it will create the file otherwise it will open it. On line 38 I create the table BloodPressure, notice how convenient this syntax is. On line 40 I instantiate a DataSource object, register for the RowSelectedEvent and assign the datasource object to the TableView.Source.
On line 48 I define the HandleDataSourceRowSelectedHandler, this method is called when a table view row is selected. First I instantiate a AddBloodPressureViewController object and set its database connection. I query for the BloodPressure selected database record, set the PressureObject property and navigate to the view.
On line 65 I define the method InsertNewObject and decorate the method with the [Export(“InsertNewObject”] attribute. This Export attribute is use here because the method is used by a Selector object. In this method I instantiate a AddBloodPressureViewController, set the database connection property, subscribe for the SavedEvent and finally navigate to the view.
On line 75 is the handler for the save event. Here a unsubscribe for the SavedEven, call ReloadData on TableView object and navigate back to the first view. Now lets move on to the DataSource class, as you can see this a very common design pattern used in mono touch. Most of the methods discussed here were explained in my previous article. The method CommitEditingStyle is implemented because I am allowing inserting, deleting and editing of tableview rows, therefore here we handle the Delete case. Once a row is deleted we need to remove such row from the SQLite database, therefore I query for the deleted row and delete the record. The last thing I do, is delete the row from the tableView using a Fade animation. The last method to discuss in this class is the RowSelected method. This method simply call a RowSelectedEvent to notify subscribers of the event.
Now lets create a new UIViewController for user interface and name it AddBloodPressureViewController. Open AddBloodPressureViewController.xib in interface builder by double clicking on the file. Create the user interface displayed in Figure above (see this blog post). Save your work.
The ViewDidLoad method I instantiate a textFieldDelegate object and assign it to the three text fields as shown in lines 57 - 59. The TextFieldDelegate class has an EditingStartedEvent the the AddBloodPressureViewController subscribes for. I add the saveButton to the navigation bar and therefore create a Selector object for the saveNewObject method. I then proceed to set the initial state for the object depending if a new object is created or not.
In the HandleTextFieldEditingStartedEvent handler I enable the respective boolean for each textfield and once each text field has been modified, the Save button is enabled.
The method ResignToKeyboard will resign first responder when the save button is handled.
The SaveNewObject method will call ResignToKeyboard, will check if the object being saved is a new object (i.e. null) or if the object is to be updated and finally it will call the SavedEvent.
Hope this helps.
The code can be downloaded here.