简体   繁体   English

TableView 在 iOS 设备中不起作用

[英]TableView is not working in iOS device

I am working on Xamarin iOS app development project through visual studio..But I am facing the problem defined below:我正在通过 Visual Studio 进行 Xamarin iOS 应用程序开发项目。但我面临以下定义的问题:

When I select a tableView from toolbox in visual studio.and populating data with UITableViewCell .It is not deploying on device,Only the plain table view is showing without any data parsed from xml.I have written the following code under RootViewController.cs class file.当我从 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);}

I cannot understanding what i am missing.我无法理解我错过了什么。

UPDATE更新

My TableSource.cs class looks like...我的 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);
        }
    }
}

Any help is appreciated..任何帮助表示赞赏..

in your Source class's constructor在你的 Source 类的构造函数中

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

you are not assigning _tableItems so it will always be empty.您没有分配 _tableItems,因此它将始终为空。 Add this添加这个

_tableItems = items;

like you have in your other constructor overload就像你在其他构造函数重载中一样

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

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