简体   繁体   中英

Display JSON on UITableView w/ Xamarin.iOS

I am trying to display the captions of the Instagram Pictures from my feed onto a UITableView using Xamarin.iOS. Currently I am trying to load the Data via this function:

// The Request
            var request = new OAuth2Request ("GET", new Uri (defaultURL + "users/self/feed"), null, instagram);
            request.GetResponseAsync ().ContinueWith (response => {
                if (response.IsFaulted) {
                    Console.WriteLine ("Error: " + response.Exception.InnerException.Message);
                } else {
                    using (var stream = new StreamReader (response.Result.GetResponseStream ())) {
                        var json = (JsonObject)JsonObject.Load (stream);
                        // Console.WriteLine (json);
                        var jsonVal = JsonValue.Parse (json);
                        var username = jsonVal["data"]["username"];
                        // Console.WriteLine (username);
                        for (var i = 0; i < 11; i++) {
                            tableData.Add (new Instagram_Picture () {
                                Name = json ["data"]["username"]
                            });
                        }               
                    }
                }
            });
table.Source = new TableSource (tableData.ToArray ());
        table.ReloadData ();

I am using Xamarin.Auth to load the Instagram Account, and it loads perfectly and I can display the response in the Console. Here is how I am setting up the UITableView:

Instagram_Picture[] _category;
        public TableSource (Instagram_Picture[] category)
        {
            _category = category;
        }

        public override int RowsInSection (UITableView tableview, int section)
        {
            return _category.Length;
        }

        public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
        {
            UITableViewCell cell = tableView.DequeueReusableCell("TableCell");
            if (cell == null) {
                cell = new UITableViewCell (UITableViewCellStyle.Subtitle, "TableCell");
            }
            //show title
            cell.TextLabel.Text = _category [indexPath.Row].Name;
            //add accessory
            cell.Accessory = UITableViewCellAccessory.DetailDisclosureButton;
            return cell;
        }   

I have created a Instagram_Picture class which is loaded within both of these other classes. However the real problem with all of this is that nothing is actually displaying on the UITableView. Your help will be greatly appreciated and Thanks beforehand!

Try adding an override for the NumberOfSections:

public override int NumberOfSections(UITableView tableview)
{
  return 1;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM