繁体   English   中英

TableView 在 iOS 设备中不起作用

[英]TableView is not working in iOS device

我正在通过 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);
        }
    }
}

任何帮助表示赞赏..

在你的 Source 类的构造函数中

public TableSource (string[] items, RootViewController owner)
{
  this._owner = owner;
}

您没有分配 _tableItems,因此它将始终为空。 添加这个

_tableItems = items;

就像你在其他构造函数重载中一样

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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