【问题标题】:Error on app launch: Foundation.MonoTouchException: Objective-C exception thrown (Xamarin iOS)应用启动时出错:Foundation.MonoTouchException:引发了 Objective-C 异常(Xamarin iOS)
【发布时间】:2016-07-27 08:30:16
【问题描述】:

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

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

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

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

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

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

视图控制器(基础,一切都在其中)具有“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();
        }
    }
}

【问题讨论】:

    标签: c# ios uitableview xamarin


    【解决方案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 的任何问题。

    【讨论】:

    • 有点帮助,但不能解决我的问题。我不知道如何更好地描述我的情况。不过谢谢!
    • 您能在某处分享您的解决方案吗?或者给我更多的细节来帮助你。顺便说一句,您不应该将 UITableView 作为 UITableSource 中的属性。
    • 再次感谢您的回复。我确实设法采取了另一种方式来解决我的“任务”。我使用了 Xamarin 的一个示例并围绕它进行构建。下面是例子:developer.xamarin.com/recipes/ios/content_controls/tables/…我现在没有问题了。如果我再次遇到此问题(或类似问题),是否有办法以某种方式向您发送消息?因为您似乎了解 Xamarin。再次感谢您的努力! ps:我将你的帖子标记为答案,因为如果我仍然有问题,你会帮助我解决它。
    • 很高兴知道您解决了您的问题,如果您有任何问题,请直接提出。我不确定堆栈是否允许用户留下他们的联系信息。
    猜你喜欢
    • 2018-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多