ios - TableView 在 iOS 设备上不工作

标签 ios uitableview ipad visual-studio-2012 xamarin

我正在通过 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/

相关文章:

iphone - 从 OS X 访问 iOS 应用文档目录

ios - 雅虎 OAuth 错误 401 消费者 key 未知

ios - 如何通过 zRotation 约束阻止我的角色过度旋转?

ios - 如何调整自定义 UITableViewCell 的大小以适应内容?

ios - 根据不同的UITableViewCell点击加载不同的URL-iOS

iphone - 有适用于 iPhone/iPod touch 和 iPad 的物理引擎 SDK 吗?

javascript - iPad 上的 HTML5 视频 "Black Screen"

ios - iPhone 何时会在状态栏中显示固定位置图标?

ios - Xcode 10 beta 6 Whole Module 设置导致 "cannot have input files with file list"错误

ios - 核心数据与 NSFetchedResultsController :Invalid update with with a property in fetchObjects