我正在通过 visual studio 进行 Xamarin iOS 应用程序开发项目。但是我面临着下面定义的问题:
当我从 visual studio 的工具箱中选择一个 tableView 并使用 UITableViewCell 填充数据时。它没有部署在设备上,只有普通的 TableView 没有任何显示从 xml 解析的数据。我在 RootViewController.cs 类文件下编写了以下代码。
public override void ViewDidLoad()
{
base.ViewDidLoad();
var table = new UITableView(View.Bounds); // defaults to Plain style
string[] tableItems = new string[]
{
"Vegetables", "Fruits", "Flower Buds", "Legumes", "Bulbs", "Tubers"
};
table.Source = new TableSource(tableItems,this);
Add(table);}
我不明白我错过了什么。
更新
我的 TableSource.cs 类看起来像...
using Foundation;
using System;
using System.CodeDom.Compiler;
using UIKit;
namespace App6
{
// ReSharper disable once ClassNeverInstantiated.Global
public partial class TableSource : UITableViewSource
{
readonly string[] _tableItems;
string CellIdentifier = "TableCell";
readonly RootViewController _owner;
public TableSource(string[] items)
{
_tableItems = items;
}
public TableSource (string[] items, RootViewController owner)
{
this._owner = owner;
}
public override nint RowsInSection(UITableView tableview, nint section)
{
return _tableItems.Length;
}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
UITableViewCell cell = tableView.DequeueReusableCell(CellIdentifier);
string item = _tableItems[indexPath.Row];
//---- if there are no cells to reuse, create a new one
if (cell == null)
{ cell = new UITableViewCell(UITableViewCellStyle.Default, CellIdentifier); }
cell.TextLabel.Text = item;
return cell;
}
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
UIAlertController okAlertController = UIAlertController.Create ("Row Selected", _tableItems[indexPath.Row], UIAlertControllerStyle.Alert);
okAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
_owner.PresentViewController(okAlertController, true, null);
tableView.DeselectRow (indexPath, true);
}
}
}
感谢任何帮助..
最佳答案
在您的 Source 类的构造函数中
public TableSource (string[] items, RootViewController owner)
{
this._owner = owner;
}
您没有分配 _tableItems,因此它将始终为空。添加这个
_tableItems = items;
就像您在其他构造函数重载中一样
关于ios - TableView 在 iOS 设备上不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31185179/