Xamarin.ios monotouch UITableViewDelegate RowSelected事件无法使用?
我使用的是自定义UITableViewDelegate,在我的控制器中,我希望在tableview选中行时运行一些代码。我注意到UITableViewDelegate已经有一个名为RowSelected的事件,但您不能使用它,我猜是因为UITableViewDelegate中有一个名称完全相同的方法 如果我写: mytableviewdelegate.RowSelected+=myeventhandler 这将不会编译,并给出错误: “无法分配给‘RowSelected’,因为它是‘方法组’”Xamarin.ios monotouch UITableViewDelegate RowSelected事件无法使用?,xamarin.ios,Xamarin.ios,我使用的是自定义UITableViewDelegate,在我的控制器中,我希望在tableview选中行时运行一些代码。我注意到UITableViewDelegate已经有一个名为RowSelected的事件,但您不能使用它,我猜是因为UITableViewDelegate中有一个名称完全相同的方法 如果我写: mytableviewdelegate.RowSelected+=myeventhandler 这将不会编译,并给出错误: “无法分配给‘RowSelected’,因为它是‘方法组’”
有什么想法吗?我有一个很好的解决办法,所以我真的想弄清楚这是否是MonoTouch中的一个bug?您是如何实现自定义UITableViewDelegate的?我建议使用Monotouch的
UITableViewSource
,因为它将UITableViewDataSource
和UITableViewDelegate
合并到一个文件中,这使事情变得更加简单
一些示例代码:
(在包含UITableView
的UIViewController
中)
然后,您需要为此创建一个新类:
public class CustomTableSource : UITableViewSource
{
public CustomTableSource()
{
// constructor
}
// Before you were assigning methods to the delegate/datasource using += but
// in here you'll want to do the following:
public override int RowsInSection (UITableView tableView, int section)
{
// you'll want to return the amount of rows you're expecting
return rowsInt;
}
// you will also need to override the GetCells method as a minimum.
// override any other methods you've used in the Delegate/Datasource
// the one you're looking for in particular is as follows:
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
// do what you need to here when a row is selected!
}
}
这应该能帮助你开始。在UITableViewSource
类中,您可以始终键入public override
,MonoDevelop将向您显示哪些方法可以被覆盖
public class CustomTableSource : UITableViewSource
{
public CustomTableSource()
{
// constructor
}
// Before you were assigning methods to the delegate/datasource using += but
// in here you'll want to do the following:
public override int RowsInSection (UITableView tableView, int section)
{
// you'll want to return the amount of rows you're expecting
return rowsInt;
}
// you will also need to override the GetCells method as a minimum.
// override any other methods you've used in the Delegate/Datasource
// the one you're looking for in particular is as follows:
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
// do what you need to here when a row is selected!
}
}