【发布时间】:2015-07-02 12:51:18
【问题描述】:
我正在通过 Visual Studio 开发 Xamarin iOS 应用程序开发项目。但我面临以下定义的问题:
当我从 Visual Studio 的工具箱中选择 tableView 并使用 UITableViewCell 填充数据时。它未部署在设备上,仅显示普通表格视图而没有任何从 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);
}
}
}
任何帮助表示赞赏..
【问题讨论】:
-
你的 TableSource 类是什么样的?
-
@Jason 我已经附上了我的 TableSource 类
标签: ios uitableview ipad visual-studio-2012 xamarin