在带有Monotouch的UITableView上加载JSON

时间:2014-06-02 04:17:30

标签: ios json uitableview xamarin.ios xamarin

我正在使用Xamarin.iOS返回一个JSON并尝试将它放在UITableView上,但是我要覆盖Instagram客户端类中的一个Datum类中的ToString(),我希望能够修复这样我就可以装载的不只是他们的名字......

以下是 Instagram客户端,它将JSON转换为类对象:

public class RootObject
{
    public Pagination pagination { get; set; }
    public Meta meta { get; set; }
    public List<Datum> data { get; set; }
}

public class Datum
{
    public object attribution { get; set; }
    public List<string> tags { get; set; }
    public string type { get; set; }
    public object location { get; set; }
    public Comments comments { get; set; }
    public string filter { get; set; }
    public string created_time { get; set; }
    public string link { get; set; }
    public Likes likes { get; set; }
    public Images images { get; set; }
    public List<object> users_in_photo { get; set; }
    public Caption caption { get; set; }
    public bool user_has_liked { get; set; }
    public string id { get; set; }
    public User user { get; set; }
    public Videos videos { get; set; }


             // This is the Overridden method I want to get rid of

    public override string ToString()
    {
        if (user == null)
        {
            return "User is null";
        }
        return user.full_name;
    }
}

public class Videos
{
    public LowResolution2 low_resolution { get; set; }
    public StandardResolution2 standard_resolution { get; set; }
}

public class StandardResolution2
{
    public string url { get; set; }
    public int width { get; set; }
    public int height { get; set; }
}

public class LowResolution2
{
    public string url { get; set; }
    public int width { get; set; }
    public int height { get; set; }
}

public class User
{
    public string username { get; set; }
    public string website { get; set; }
    public string profile_picture { get; set; }
    public string full_name { get; set; }
    public string bio { get; set; }
    public string id { get; set; }
}

public class Caption
{
    public string created_time { get; set; }
    public string text { get; set; }
    public From from { get; set; }
    public string id { get; set; }
}

public class From
{
    public string username { get; set; }
    public string profile_picture { get; set; }
    public string id { get; set; }
    public string full_name { get; set; }
}

public class Images
{
    public LowResolution low_resolution { get; set; }
    public Thumbnail thumbnail { get; set; }
    public StandardResolution standard_resolution { get; set; }
}

public class StandardResolution
{
    public string url { get; set; }
    public int width { get; set; }
    public int height { get; set; }
}

public class Thumbnail
{
    public string url { get; set; }
    public int width { get; set; }
    public int height { get; set; }
}

public class LowResolution
{
    public string url { get; set; }
    public int width { get; set; }
    public int height { get; set; }
}

public class Likes
{
    public int count { get; set; }
    public List<Datum2> data { get; set; }
}

public class Datum2
{
    public string username { get; set; }
    public string profile_picture { get; set; }
    public string id { get; set; }
    public string full_name { get; set; }
}

public class Comments
{
    public int count { get; set; }
    public List<object> data { get; set; }
}

public class Meta
{
    public int code { get; set; }
}

public class Pagination
{
    public string next_url { get; set; }
    public string next_max_id { get; set; }
}

主视图控制器

这就是我返回JSON的方式

var request = new RestRequest { RootElement = "data", Resource = "/users/self/feed" };
            request.AddParameter ("access_token", instagramAccessToken);

            var client = new RestClient ("https://api.instagram.com/v1");
            client.ExecuteAsync (request, response => {
                var rootObject = JsonConvert.DeserializeObject<RootObject> (response.Content);
                table.InvokeOnMainThread (() => {
                    table.Source = new TableSource<Datum>(rootObject.data);
                    table.ReloadData ();
                });
            }
        );

TableSource类

            public List<T> Data { get; set; }
    protected string cellIdentifier = "TableCell";

    public TableSource ()
    {
        Data = new List<T> ();
    }

    public TableSource(List<T> data)
    {
        Data = data;
    }

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

    public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
    {
        if (OnRowSelected != null) {
            OnRowSelected (this, new RowSelectedEventArgs (tableView, indexPath));
        }
    }

    public class RowSelectedEventArgs : EventArgs
    {
        public UITableView tableView { get; set; }
        public NSIndexPath indexPath { get; set; }

        public RowSelectedEventArgs(UITableView tableView, NSIndexPath indexPath) : base()
        { 
            this.tableView = tableView;
            this.indexPath = indexPath;
        }
    }

    public event EventHandler<RowSelectedEventArgs> OnRowSelected;

    public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
    {

        if (tableView.ContentSize.Height - UIScreen.MainScreen.Bounds.Height <= tableView.ContentOffset.Y) {
            // this is the last row in the last section
            Console.WriteLine ("~~~Bottom~~~");
        }

        UITableViewCell cell = tableView.DequeueReusableCell (cellIdentifier);
        // if there are no cells to reuse, create a new one
        if (cell == null)
            cell = new UITableViewCell (UITableViewCellStyle.Default, cellIdentifier);

        // Edited Text 
        cell.TextLabel.Text = Data[indexPath.Row].ToString ();

        return cell;
    }

我没有真正有一种在桌面上显示JSON的具体方法,所以我会对激进的改变持开放态度。

1 个答案:

答案 0 :(得分:0)

在您的GetCell方法中,您可以访问数据的任何属性。如果要显示两行以上的数据,可能需要创建自定义UITableViewCell。

var item = Data[indexPath.Row];
cell.TextLabel.Text = item.User.username + " (" + item.User.full_name + ")";
cell.DetailTextLabel.Text = "Created at: " + item.created_time;