应用程序启动时出错:Foundation.MonoTouchException:抛出Objective-C异常(Xamarin iOS)

时间:2016-07-27 08:30:16

标签: c# ios uitableview xamarin

启动我的应用时收到此错误消息:

  

Foundation.MonoTouchException:抛出Objective-C异常。名称:   NSInvalidArgumentException原因: - [TableSource initWithCoder:]:   无法识别的选择器发送到实例0x796e6fa0

我已经在google上搜索过,但没有找到解决方案。

关于应用程序:该应用程序有一个带有一些自定义单元格的UITableView。 UITableView是一个普通的“视图”。还有正常“视图”上的按钮,此按钮应该(当被触摸时)向UITableView添加自定义单元格。

UITableView的名称为“tableView”,属性为“TableSource”类。 Button的名称为“btn01”,属性为“ViewController”类。

自定义单元格具有“重用标识符”“Cell01Reuse”,“Cell02Reuse”等。并且具有类“Testclass”(它不作为文件存在)。

View Controller(基础,一切都在其中)将“ViewController”作为类。

我有两个代码类。首先是“查看控制器”:

using System;
using UIKit;
using Foundation;
using System.Collections.Generic;

namespace myapp
{
    public partial class ViewController : UIViewController
    {
        public ViewController(IntPtr handle) : base(handle)
        {
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            // Perform any additional setup after loading the view, typically from a nib.

            //UITableView _table;
            //_table = new UITableView
            //{
            //    Frame = new CoreGraphics.CGRect(0, View.Bounds.Height * 0.03, View.Bounds.Width, View.Bounds.Height * 0.80),
            //    Source = new TableSource(null)
            //};
            //_table.SeparatorStyle = UITableViewCellSeparatorStyle.None;
            //View.AddSubview(_table);

            TableSource TS = new TableSource();

            btn01.TouchUpInside += (sender, e) =>
            {
                TS.updateTableView();
                string cell01 = "Cell01Reuse";
                TS.tableItems.Add(cell01);
            };
        }

        public override void DidReceiveMemoryWarning()
        {
            base.DidReceiveMemoryWarning();
            // Release any cached data, images, etc that aren't in use.
        }
    }
}

其次是“TableSource”:

using System;
using System.Collections.Generic;
using System.Text;
using Foundation;
using UIKit;

namespace myapp
{
    public partial class TableSource : UITableViewSource
    {



        //string[] tableItems;
        public List<string> tableItems = new List<string>();
        public static string cellIdentifier = "TableCell";

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

        public TableSource()
        {
        }

        public override nint RowsInSection(UITableView tableview, nint section)
        {
            return 0;
        }

        public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
        {
            UITableViewCell cell = tableView.DequeueReusableCell(cellIdentifier);
            if (cell == null)
            {
                cell = new UITableViewCell(UITableViewCellStyle.Default, cellIdentifier);
            }
            cell.TextLabel.Text = tableItems[indexPath.Row];
            tableItems.Add(Convert.ToString(cell));
            return cell;
        }

        public override nint NumberOfSections(UITableView tableView)
        {
            return base.NumberOfSections(tableView);
        }

        public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
        {
            new UIAlertView("Alert", "You touched: " + tableItems[indexPath.Row], null, "OK", null).Show();
            tableView.DeselectRow(indexPath, true);
        }

        public void updateTableView()
        {
            tableView.updateTableView();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

其实我无法重现你遇到的问题,你的描述不够清楚,但你的代码肯定存在一些问题,比如你不应该在RowsInSection方法中返回0,我可以给你一个示例参考。(所有UI都是由代码创建的)

public partial class ViewController : UIViewController
{
    protected ViewController (IntPtr handle) : base (handle)
    {
        // Note: this .ctor should not contain any initialization logic.
    }

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();
        // Perform any additional setup after loading the view, typically from a nib.

        CGRect tableFrame = this.View.Bounds;
        tableFrame.Y = 100;
        tableFrame.Height -= 100;
        UITableView tableView = new UITableView (tableFrame);
        this.View.AddSubview (tableView);

        MyTalbeSource mySource = new MyTalbeSource ();
        tableView.Source = mySource;
        tableView.ReloadData ();

        int count = 0;
        UIButton btnNew = new UIButton (UIButtonType.System);
        btnNew.Frame = new CGRect (20, 20, 100, 40);
        btnNew.SetTitle ("NewItem", UIControlState.Normal);
        btnNew.TouchUpInside += delegate {
            mySource.AddNewItem ("NewItem" + count++);
            tableView.ReloadData ();
        };
        this.Add (btnNew);
    }
}

class MyTalbeSource : UITableViewSource
{
    private const string CELL_ID = "MyTalbeCell";
    private List<string> dataList;

    public MyTalbeSource ()
    {
        dataList = new List<string> ();
        for (int i = 0; i < 5; i++) {
            dataList.Add ("Test " + i.ToString ());
        }
    }

    public void AddNewItem (string title)
    {
        dataList.Add (title);
    }

    public override nint RowsInSection (UITableView tableview, nint section)
    {
        return dataList.Count;
    }

    public override UITableViewCell GetCell (UITableView tableView, Foundation.NSIndexPath indexPath)
    {
        MyTableCell cell = tableView.DequeueReusableCell (CELL_ID) as MyTableCell;
        if (null == cell) {
            cell = new MyTableCell (UITableViewCellStyle.Default, CELL_ID);
            cell.InitCell ();
        }
        cell.Text = dataList [indexPath.Row];
        return cell;
    }
}

class MyTableCell : UITableViewCell
{
    private UILabel lbInfo;

    public string Text { 
        get {
            return lbInfo.Text;
        }
        set {
            lbInfo.Text = value;
        }
    }

    public MyTableCell (UITableViewCellStyle style, string cellID) : base (style, cellID)
    {
    }

    public void InitCell ()
    {
        lbInfo = new UILabel ();
        lbInfo.TextAlignment = UITextAlignment.Center;
        this.AddSubview (lbInfo);
    }

    public override void LayoutSubviews ()
    {
        lbInfo.Frame = this.Bounds;
    }
}

希望它可以帮到你。

欢迎任何有关Xamarin.iOS的问题。