2012-04-23 83 views
6

我將本機Objective-C應用程序移植到Monotouch解決方案。MonoTouch如何爲具有靜態單元的UITableViewController實現RowSelected

我在故事板中有一個tableview,有一些靜態表格單元格。

使用以下Objective-C代碼一個接一個選擇從表

- (void) tableView:(UITableView *) aTableView didSelectRowAtIndexPath:(NSIndexPath *) indexPath 
{  // remove the row highlight 
    [aTableView deselectRowAtIndexPath:indexPath animated:YES]; 

    switch(indexPath.row) { 
     case 0: [self callWebsite]; break; 
     case 1: [self sendEmail]; break; 
     case 2: [self makePhoneCall]; break; 
    } 

    return; 
} 

現在我想要做的MonoTouch是相同的,但不能找出我如何和所有的例子在網絡上找到,正在使用DataSource和UITableViewDelegate。

但是,當使用這種方法時,我的「靜態」單元被刪除,替換。

這就是我想要

public partial class ContactViewController : UITableViewController 
{ 
    public ContactViewController (IntPtr handle) : base (handle) 
    { 
     this.TableView.Delegate = new CustomTableViewDelegate(); 
    } 
} 

public class CustomTableViewDelegate : UITableViewDelegate 
{ 
    public override void RowSelected (UITableView tableView, NSIndexPath indexPath) 
    { 
     // Remove the row highlight 
     RowDeselected(tableView, indexPath); 

     /* 
      // Is this section our "button" section? 
      switch(indexPath.row) { 
       case 0: [self callWebsite]; break; 
       case 1: [self sendEmail]; break; 
       case 2: [self makePhoneCall]; break; 
      } 
     */ 

     Console.WriteLine("Do something meaningfull."); 
    } 
} 

有人什麼建議嗎?

+0

我不緊隨其後,你的細胞如何去除? 您粘貼的代碼是用於選中單元格時的操作,並且您有一些註釋代碼,爲什麼要註釋掉它? – 2012-04-23 21:10:25

+0

註釋掉的代碼是我的objective-c代碼的複製粘貼,因爲我正在將它轉換爲C#。對這個問題沒有任何重要性。我不知道爲什麼細胞被移除。我認爲這是因爲我沒有實現從代碼創建表時使用的方法'numberOfRowsInSection'和'cellForRowAtIndexPath'。不是靜態的......「我想......」 – user1346056 2012-04-24 12:24:32

回答

7

我認爲你應該嘗試使用WeakDelegate來完成這項工作。

在您的控制器的電纜鋪設方法,像這樣:

[Export("tableView:didSelectRowAtIndexPath:")] 
public void RowSelected (UITableView tableView, NSIndexPath indexPath) 
{ 
    //Your code here 
} 

然後設置TableView.WeakDelegate = this;

玩弄它,我可能沒有我的參數完全正確的,等

+0

我測試了這一點,發現設置WeakDelegate導致靜態單元被清除。但是,如果使用Export屬性將RowSelected方法添加到擁有的UITableViewController中,那麼您將得到預期的結果。 – holmes 2012-04-24 02:24:52

+0

因此,您設置的靜態單元必須將控制器設置爲「UITableView」的委託。你所做的可能是使其與MonoTouch協同工作的唯一方法。這是我想嘗試一件事情的一件事,我從未使用故事板,因爲他們需要iOS 5.我們已經到了需要iOS 5的地步了。 – jonathanpeppers 2012-04-24 12:03:39

+0

確實實現這一點,沒有弱代理作業。 Thx傢伙!我會投票,但我沒有足夠的聲望,whoehoeee = P – user1346056 2012-04-24 12:14:49

相關問題