ios - Xamarin TableView 崩溃

标签 ios uitableview xamarin xamarin.ios

我想在 XamarinApp 上使用 UITableView

我尝试了 UITableView 示例 Populating a Table with Data , 但它不起作用。

当我使用 this.Add(table); 时导致崩溃。当我删除 this.Add(table) 时,它显示空表。

请帮帮我...

这是我的代码

using Foundation;
using System;
using System.CodeDom.Compiler;
using UIKit;

namespace KUkyuko
{
    partial class MyTableViewSource : UITableView
    {
        public MyTableViewSource(IntPtr handle) : base (handle)
        {
            var table = this;
            string[] tableItems = new string[] {"Vegetables","Fruits","Flower Buds","Legumes","Bulbs","Tubers"};
            table.Source = new TableSource(tableItems);
            this.Add(table); //this code cause crash
        }
    }
    public class TableSource : UITableViewSource {

        string[] TableItems;
        string CellIdentifier = "TableCell";

        public TableSource (string[] items)
        {
            TableItems = items;
        }

        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;
        }
    }
}

最佳答案

我不确定你想达到什么目的但是

我会像这样将 tableview 更改为 tableViewController::

partial class TableViewController : UITableViewController
{
    public TableViewController (IntPtr handle) : base (handle)
    {
        // Register the TableView's data source
        string[] tableItems = new string[] {"Vegetables","Fruits","Flower Buds","Legumes","Bulbs","Tubers"};
        this.TableView.Source = new TableSource(tableItems);
    }
}

public class TableSource : UITableViewSource {

    string[] TableItems;
    string CellIdentifier = "TableCell";

    public TableSource (string[] items)
    {
        TableItems = items;
    }

    public override nint RowsInSection (UITableView tableview, nint section)
    {
        return TableItems.Length;
    }

    public override nint NumberOfSections (UITableView tableView)
    {
        // TODO: return the actual number of sections
        return 1;
    }

    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;
    }
}

如果您在 viewController 中使用表格,那么您需要在 viewController 中执行此部分并为表格设置导出。

当您将此代码添加到此时此代码崩溃的原因:

 var table = this;
 string[] tableItems = new string[] {"Vegetables","Fruits","Flower Buds","Legumes","Bulbs","Tubers"};
 table.Source = new TableSource(tableItems);
 this.Add(table); //this code cause crash as it is the same as this.Add(this)

希望这对您有所帮助!

关于ios - Xamarin TableView 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30702078/

相关文章:

ios - swift:motionEnded 不触发或真实设备

objective-c - 调用时 UITableView 部分不重新加载

ios - -webkit-溢出-滚动 : touch breaks my -webkit-scrollbar css in iOS

ios - 不下载数据获取CKAsset URL

swift - 基于按下按钮在同一个 TableView Controller 中加载不同的数组

ios - Xamarin UIButton 有多个事件处理程序

ios - 单点触控 : pause application unit dialog response

c# - sqlite 中保存的 xamarin 应用程序数据已清除

iPhone 上的 iOS 8 多个 <select> 严重错误?

UITableView 文本标签对齐方式