2015-01-08 32 views
2

在Xamarin iOS中 - 我有一個名爲TableCell (public class TableCell : UITableViewCell { })的類 - 在這個類中我聲明瞭按鈕。在xamarin iOS中 - 在UITableViewCell中獲取[indexPath.Row],使用按鈕單擊事件

在按鈕單擊事件中,我試圖訪問[indexPath.Row]UITableViewCell。我可以選擇一行以在UITableViewSource中找到[indexPath.Row]

public class TableSource : UITableViewSource { 
// - - - - 
public override void RowSelected (UITableView tableView, NSIndexPath indexPath) 
    { 
     new UIAlertView("Row Selected", tableItems[indexPath.Row], null, "OK", null).Show(); 
     tableView.DeselectRow (indexPath, true); // iOS convention is to remove the highlight 

    } 

// - - - - 

} 

我怎樣才能在UITableViewCell[indexPath.Row],使用按鈕單擊事件..?

回答

0

使用VisibleCells財產。 只要用戶選擇行,UITableViewCell就會顯示。


替代解決方案(如果你想獲得類似,但不一樣細胞): 您可以通過UITableViewCell.GetCell得到它使用傳遞UITableView tableView,然後丟在你的類型的UITableViewCell,然後調用它的方法使用通過NSIndexPath indexPath

事情是這樣的:

public class TableSource : UITableViewSource { 
    // - - - - 
    public override void RowSelected (UITableView tableView, NSIndexPath indexPath) 
     { 
     var cell = tableView.GetCell(tableView, indexPath); 
     var myCell class = cell as MyCellClass; 
     if (myCell != null) { 
      // TODO: do something with cell. 
     }   
     } 
    // - - - - 
    } 
// - - - - 
} 
0

這是不是最好的方法,但它肯定會工作:

我將延長的TableCell類新的屬性:

public NSIndexPath IndexPath { get; set; } 

然後我會添加這行到UITableViewSource的GetCell方法,如果你是你單元格重複使用模式:

public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) 
    { 
     TableCell cell = tableView.DequeueReusableCell("YourIdentifier") ?? new TableCell(); 
     cell.IndexPath = indexPath; 
     return cell; 
    } 

然後,您可以訪問單元格當前的任何IndexPath。即

public TableCell() 
{ 
    UIButton btn = new UIButton(UIButtonType.RoundRect); 
    btn.TouchUpInside += (sender, args) 
    { 
      new UIAlertView("Button pressed!", indexPath.Row, null, "OK", null).Show(); 
    } 
} 
+0

嗨,我仍然通過你提供的代碼,並與一些新的類,我欣賞的幫助。謝謝,我們很快就會回來。 –

+0

如果您需要更多幫助,請隨時與我聯繫:) – Roosevelt