在MonoTouch的填充一个简单的UITableView与UITableViewDataSourc

2019-09-17 01:29发布

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

Answer 1:

UITableView ,以及相关的委托和数据源类型,是不是一个非常友好的API -甚至更少为典型的.NET开发人员。 有需要的时候它的时间,但是,在大多数情况下,你会希望使用更简单的东西。

创建项目列表的项目分配到的UITableView的该列表允许用户滚动列表。 寻找一个非常非常简单的例子。

这似乎符合MonoTouch.Dialog的目标。 该组件现在随MonoTouch的,但你可以找到示例源代码在GitHub上。

Xamarin的文档的网站也有一个教程使用MonoTouch.Dialog和一个不错的视频也可以。



文章来源: Populating a simple UITableView in MonoTouch with UITableViewDataSource - how to use UITableViewDataSource