Populating a simple UITableView in MonoTouch with

2019-06-17 04:08发布

问题:

I'm loving MonoTouch so far, but find the documentation and samples lacking once you get past the few beginner lessons.

I am struggling with a simple concept of populating a simple UITableView in MonoTouch. I have seen examples of doing this with custom cells, and extensively customizing the list with varying pieces of information, but I am just looking for the bare minimum of how to achieve this simple thing. I'm a professional .NET developer and very new to iOS, so still wrapping my head around how it works.

I just want the following. Create a list of items Assign that list of items to the UITableView to allow the user to scroll the list. Looking for a very very simple example.

The biggest thing I am struggling with is creating a UITableViewDataSource object. It's abstract so I can't create an instance of it. I could create my own class that inherits it, but I am looking for a concrete implementation in the framework to use without having to create my own. Am I too spoiled by .NET and have to create a new class for it, or does it exist somewhere in the Framework and I just can't find it?

[Answer here] I found an answer, but StackOverflow won't let me answer my own question with less than 100 reputation or unless I wait 8 hours which I am not willing to do. Answer below:

I created the following class that inherits UITableViewSource. It is a requirement to do it this way. GetCell controls how you will render the cell on the table.

Create an instance of this and pass in a list of strings to populate.

This example came from (I modified it slightly) the book I am reading from Wrox Press - "Professional iPhone Programming with MonoTouch and .NET C#"

public class TableViewSource : UITableViewSource
{
    private List<string> rows;

    public TableViewSource (List<string> list) 
    { 
            rows = list; 
    }

    public override int RowsInSection (UITableView tableview, int section) 
    { 
        return rows.Count; 
    }

    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
    {  
        UITableViewCell cell = new UITableViewCell(UITableViewCellStyle.Default,"mycell"); 
        cell.TextLabel.Text = rows[indexPath.Row];
        return cell;
    } 
}

Put the following code in ViewDidLoad on your screen.

var list = new List<String>()
{   
    "San Francisco",    
    "Buenos Aires", 
    "São Paulo"
};

TableViewSource tableData = new TableViewSource(list);
this.tblTrips.Source = tableData;

This gives me what I am looking for without requiring a deep dive into MonoTouch.Dialog

回答1:

UITableView, and associated delegate and datasource types, is not a very friendly API - even less for the typical .NET developer. There are times when it's needed but, in most cases, you'll want to use something simpler.

Create a list of items Assign that list of items to the UITableView to allow the user to scroll the list. Looking for a very very simple example.

That seems to fit MonoTouch.Dialog's goal. The assembly is now shipped with MonoTouch but you can find the sample source code on github.

Xamarin's documentation web site also has a tutorial for using MonoTouch.Dialog and a nice video is also available.