簡體   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