gpt4 book ai didi

c# - iOS C# 将 JSON 数据添加到 tableView

转载 作者:行者123 更新时间:2023-11-29 05:24:21 25 4
gpt4 key购买 nike

我无法弄清楚这一点。似乎所有数据都进入了 CountryNames 列表,但在函数 GetCountries() 之外,列表显示为空。任何帮助将不胜感激。当我在 GetCountries 函数中将列表打印到控制台时,它会打印出所有数据,但列表之外是空的。

namespace Test
{
public partial class StartViewController : UIViewController
{
private List<String> countryNames;
//private List<String> countryLanguages;

public StartViewController() : base("StartViewController", null)
{
countryNames = new List<String>();
GetCountries();

// function to get country names and languages
async Task GetCountries()
{
List<CountryInfo> countries;
countries = await RefreshDataAsync();

foreach (var c in countries)
{
countryNames.Add(c.name);
}
}

}

public override void ViewDidLoad()
{
base.ViewDidLoad();
// Perform any additional setup after loading the view, typically from a nib.
CountriesTableView.Source = new StartViewController.TableSource(countryNames, this);
}

public override void DidReceiveMemoryWarning()
{
base.DidReceiveMemoryWarning();
// Release any cached data, images, etc that aren't in use.
}

public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);
}

#region TableView source stuff
public class TableSource : UITableViewSource
{
StartViewController vc;
private List<String> countries;
//private List<String> languages;

public TableSource(List<String> countriesIn, StartViewController vc_in)
{
countries = countriesIn;
vc = vc_in;
}

public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
UITableViewCell cell;
// try to get a reusable cell
cell = tableView.DequeueReusableCell("countries");
if (cell == null)
{
cell = new UITableViewCell( UITableViewCellStyle.Subtitle, "countries");
}

return cell;
}

public override nint NumberOfSections(UITableView tableView)
{
return 1;
}

public override string TitleForHeader(UITableView tableView, nint section)
{
return " ";
}

public override nint RowsInSection(UITableView tableview, nint section)
{
return 1;
}
}
#endregion

// A task that calls other awaitable async methods
public async Task<List<CountryInfo>> RefreshDataAsync()
{

HttpClient client;
client = new HttpClient();

var uri = new Uri("https://restcountries.eu/rest/v2/all");


var response = await client.GetAsync(uri);
List<CountryInfo> Items = null;
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();


Items = JsonConvert.DeserializeObject<List<CountryInfo>>(content);
}

return Items;
}

#region Json helpers use http://json2csharp.com/ paste in json or url of service and it generates classes for you
public class Currency
{
public string code { get; set; }
public string name { get; set; }
public string symbol { get; set; }
}

public class Language
{
public string iso639_1 { get; set; }
public string iso639_2 { get; set; }
public string name { get; set; }
public string nativeName { get; set; }
}

public class Translations
{
public string de { get; set; }
public string es { get; set; }
public string fr { get; set; }
public string ja { get; set; }
public string it { get; set; }
public string br { get; set; }
public string pt { get; set; }
public string nl { get; set; }
public string hr { get; set; }
public string fa { get; set; }
}

public class CountryInfo
{
public string name { get; set; }
public List<string> topLevelDomain { get; set; }
public string alpha2Code { get; set; }
public string alpha3Code { get; set; }
public List<string> callingCodes { get; set; }
public string capital { get; set; }
public List<object> altSpellings { get; set; }
public string region { get; set; }
public string subregion { get; set; }
public int population { get; set; }
public List<object> latlng { get; set; }
public string demonym { get; set; }
public double? area { get; set; }
public double? gini { get; set; }
public List<string> timezones { get; set; }
public List<object> borders { get; set; }
public string nativeName { get; set; }
public string numericCode { get; set; }
public List<Currency> currencies { get; set; }
public List<Language> languages { get; set; }
public Translations translations { get; set; }
public string flag { get; set; }
public List<object> regionalBlocs { get; set; }
public string cioc { get; set; }
}
#endregion
}

}

最佳答案

问题可能是 ViewDidLoad在您获得 countryNames 之前被调用从您的网络服务返回。 GetCountries是一个异步方法,因此它返回 Task立即,构造函数完成并立即返回,因此操作系统现在可以自由调用 ViewDidLoad ,它确实如此......但您仍在等待网络请求结果,所以 countryNames当你调用CountriesTableView.Source = new StartViewController.TableSource(countryNames, this);时仍然是一个空列表,因此countries字段也是一个空列表,这是您获取要在列表中显示的数据的位置。所以你需要在 GetCountries 时更新你的源代码完成...但是您无法执行此操作,因为您没有等待 GetCountries 的调用.

namespace Test
{
public partial class StartViewController : UIViewController
{
// No need for this here, since your TableSource will be holding
// and providing your data
//private List<String> countryNames;
//private List<String> countryLanguages;

public StartViewController() : base("StartViewController", null)
{

// Best Practice not to call async methods from a constructor
// A constructor should be fast and return, after running all
// of its code, right away.
//GetCountries();
}


public override async void ViewDidLoad()
{
base.ViewDidLoad();
// Perform any additional setup after loading the view, typically from a nib.
// Get your data and then set your source
var countries = await RefreshDataAsync();

var countryNames = new List<string>();
foreach (var c in countries)
{
countryNames.Add(c.name);
}

// since you awaited the above, countryNames is now populated
// before you set up your TableSource.
CountriesTableView.Source = new StartViewController.TableSource(countryNames, this);

// On a side note, by passing and holding onto a reference to
// this UIViewController, you are creating a circular reference
// which will cause a memory leak. You should not need a reference
// to this view controller from your TableSource.
}
...
}
...
}

就是这样,然后看看你的 TableSource代码,您没有用任何数据填充单元格。您获取并返回单元格,但没有设置任何 UI 控件,例如 UILabel s、文本(或其他)值来显示您的数据。您还需要告诉UITableView通过返回 RowsInSection 中的项目数,您必须显示多少项目方法。事实上,您正在告诉UITableView你只有一件元素。您需要执行以下操作:

    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
UITableViewCell cell;
// try to get a reusable cell
cell = tableView.DequeueReusableCell("countries");
if (cell == null)
{
cell = new UITableViewCell( UITableViewCellStyle.Subtitle, "countries");
}

// Get data for row
string countryName = countries[indexPath.Row];

// Display data in the cell
cell.TextLabel.Text = countryName;

return cell;
}

public override nint RowsInSection(UITableView tableview, nint section)
{
// return the number of items you need to display
return countries.Count;
}

如果我是你,我就会拥有你的TableSource有一个List<CountryInfo>字段而不是 List<string> .这样你的TableSource可以提供 UITableView 的任何和所有国家/地区数据使用它可能需要显示。

关于c# - iOS C# 将 JSON 数据添加到 tableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58347366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com