2011-04-05 60 views
0

我正在使用自定義的UITableViewDelegate,並且在我的控制器中,我希望在tableview有一行被選中時運行一些代碼。我注意到UITableViewDelegate已經有一個名爲RowSelected的事件,但是你不能使用它我猜測是因爲UITableViewDelegate中有一個名字完全相同的方法。monotouch UITableViewDelegate RowSelected事件不能使用?

如果我寫:

mytableviewdelegate.RowSelected + =一個MyEventHandler;

這將無法編譯,並給出錯誤:

「不能分配給‘RowSelected’,因爲它是一個‘方法組’」

任何想法,我有一個解決辦法是罰款因此Im真的在研究這是否是MonoTouch中的錯誤?

回答

2

你是如何實現自定義UITableViewDelegate的?我建議使用Monotouch的UITableViewSource,因爲它將UITableViewDataSourceUITableViewDelegate組合成一個文件,這使得事情變得更容易。

一些示例代碼:

(在你的UIViewController包含UITableView

tableView.Source = new CustomTableSource(); 

那麼你要創建一個新的類此:

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將顯示可用於覆蓋的方法。