monotouch UITableViewDelegate RowSelected 事件不能使用? [英] monotouch UITableViewDelegate RowSelected event cannot be used?

查看:25
本文介绍了monotouch UITableViewDelegate RowSelected 事件不能使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用自定义 UITableViewDelegate 并且在我的控制器中我想在 tableview 有一个 rowselected 时运行一些代码.我注意到 UITableViewDelegate 已经有一个名为 RowSelected 的事件,但我猜你不能使用它,因为 UITableViewDelegate 中有一个同名的方法.

I am using a custom UITableViewDelegate and in my controller I want to run some code when the tableview has a rowselected. I noticed the UITableViewDelegate already has an event called RowSelected but you cannot use it I'm guessing because there is a method in UITableViewDelegate with the exact same name.

如果我写:

mytableviewdelegate.RowSelected += myeventhandler;

mytableviewdelegate.RowSelected += myeventhandler;

这不会编译并给出错误:

This will not compile and gives the error:

无法分配给'RowSelected',因为它是一个'方法组'"

"Cannot assign to 'RowSelected' because it is a 'method group'"

任何想法,我有一个很好的解决方法,所以我真的在考虑解决这是否是 MonoTouch 中的错误?

Any ideas, I have a work around which is fine so Im really looking at working out if this is a bug in MonoTouch?

推荐答案

你是如何实现自定义 UITableViewDelegate 的?我建议使用 Monotouch 的 UITableViewSource,因为它将 UITableViewDataSourceUITableViewDelegate 结合到一个文件中,这使事情变得更加容易.

How are you implementing the custom UITableViewDelegate? I would suggest using Monotouch's UITableViewSource as it combines both the UITableViewDataSource and the UITableViewDelegate into one file which makes things so much easier.

一些示例代码:

(在包含 UITableViewUIViewController 中)

(in your UIViewController that contains the UITableView)

tableView.Source = new CustomTableSource();

然后您需要为此创建一个新类:

Then you'll want to create a new class for this:

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 将向您显示哪些方法可以被覆盖.

That should help you get started. In the UITableViewSource class you can always type public override and MonoDevelop will show you what methods are available to be overriden.

这篇关于monotouch UITableViewDelegate RowSelected 事件不能使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆