[MonoTouch] SQLite iOS binding to UITableView

Jeff Stedfast jeff at xamarin.com
Fri Mar 16 17:26:51 UTC 2012


Hi Jon,

Most people seem to use Frank Krueger's SQLite-Net binding as opposed to
System.Data because it's a bit simpler for most people.

As luck would have it, I'm working on trying to make a very easy to use /
generic (literally!) MonoTouch library for displaying data from a sqlite
database in a UITableViewController. I was planning about blogging about it
this weekend when I had worked out some of the remaining kinks and such,
but I can point you at it and the source code for my own personal app that
uses it (I want to write a simpler showcase app before I blog this weekend).

The SQLiteTableViewController library is at
https://github.com/jstedfast/MonoTouch.SQLite

You can see how I'm using it at
https://github.com/jstedfast/FlightLog/blob/master/FlightLog/Flights/FlightViewController.cs

It's a bit on the "advanced" side since it allows adding/deleting/editing
logbook entries, but maybe that'll be helpful to you :-)

In the simple case, you could just subclass SQLiteTableViewController<T>
and get away with just implementing the following method:

*protected override UITableViewCell GetCell (UITableView tableView,
NSIndexPath path, T item)*

So... a very simple TableViewController might look like this:

// This represents an element in your sqlite database table named "Task"
public class Task {
    [PrimaryKey][AutoIncrement]
    public int TaskId { get; set; }

    public string ToDo { get; set; }
}

public class TaskViewController : SQLiteViewController<Task>
{
    static NSString key = new NSString ("TaskCellKey");

    public TaskViewController () : base (sqlitedb, 16)
    {
    }

    protected override UITableViewCell GetCell (UITableView tableView,
NSIndexPath path, Task task)
    {
        var cell = tableView.DequeueReusableCell (key);
        if (cell == null)
            cell = new UITableViewCell (UITableViewCellStyle.Plain, key);

        cell.TextLabel.Text = "Task";
        cell.DetailTextLabel.Text = task.ToDo;

        return cell;
    }
}

The SQLiteTableViewController would then handle all of the searching logic
for you...

Hope that helps,

Jeff

On Fri, Mar 16, 2012 at 12:52 PM, JonW <jonw at dialacab.co.uk> wrote:

> Hi there. I've been playing with MonoTouch on a Mac for a few days and
> whilst
> I'm getting there with several things (web services, customising the view
> controllers etc) I am having real issues with SQLite. First off, there are
> several types of data consumption via SQLite referred to in samples and on
> other websites (SQLite-Net, Vici, MonoTouch.Data etc) - which is the
> simplest method? Basically, my app will contain UITableViews and text boxes
> and I want to be able to create my database (unless it already exists),
> create tables (unless they already exist) and populate them on button
> click,
> bind UITableViews to queries (like I would bind GridViews in .Net), update
> and delete from them by Id. Any suggestions of a good, fully working
> example
> of how to do this? I'm pulling my hair out here!
>
> Another example I need is how to bind UITableViews to the output from my
> consumed .Net web services.
>
> Thank you for any help :-)
>
> Jon
>
> --
> View this message in context:
> http://monotouch.2284126.n4.nabble.com/SQLite-iOS-binding-to-UITableView-tp4478704p4478704.html
> Sent from the MonoTouch mailing list archive at Nabble.com.
> _______________________________________________
> MonoTouch mailing list
> MonoTouch at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/monotouch
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/monotouch/attachments/20120316/88ecc8e7/attachment-0001.html>


More information about the MonoTouch mailing list