【发布时间】: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