2016-11-17 54 views
0

我已經爲我的TableView創建了Custom TableCell。在自定義單元格中,我有Button。所以我想在按鈕點擊時打開New ViewController,並在其上發送數據。所以如何在自定義TableCell中創建事件來打開新的ViewController。自定義TableCell按鈕在iOS中點擊新的ViewController(Xamarin)

我是這樣創建的這是正確的方式嗎?

我CustomCell:

public partial class CaseHistotyTableCell : UITableViewCell 
    { 
     public static readonly NSString Key = new NSString("CaseHistotyTableCell"); 
     public static readonly UINib Nib; 

     static CaseHistotyTableCell() 
     { 
      Nib = UINib.FromName("CaseHistotyTableCell", NSBundle.MainBundle); 
     } 

     protected CaseHistotyTableCell(IntPtr handle) : base(handle) 
     { 
      // Note: this .ctor should not contain any initialization logic. 
     } 

     public static CaseHistotyTableCell Create() 
     { 
      return (CaseHistotyTableCell)Nib.Instantiate(null, null)[0]; 
     } 
     internal void BindData(string hospitalLabel, string addressLabel, string drLabel, string patientLabel) 
     { 
      this.lbl_hospitalName.Text = hospitalLabel; 
      this.lbl_address.Text = addressLabel; 
      this.lbl_drName.Text = drLabel; 
      this.lbl_patientName.Text = patientLabel; 

      this.lbl_address.TextColor = UIColor.Clear.FromHexString("#000000", 0.54f); 
      this.lbl_patientName.TextColor = UIColor.Clear.FromHexString("#000000", 0.54f); 
      this.lbl_hospitalName.TextColor = UIColor.Clear.FromHexString("#000000", 0.87f); 
      this.lbl_drName.TextColor = UIColor.Clear.FromHexString("#000000", 0.87f); 
      this.btn_createAppointment.SetTitleColor(UIColor.Clear.FromHexString("#0072BA",1.0f), UIControlState.Normal); 
      this.btn_viewDetail.SetTitleColor(UIColor.Clear.FromHexString("#0072BA", 1.0f), UIControlState.Normal); 

      this.btn_viewDetail.TouchUpInside += (sender, e) => 
      { 

      }; 


     } 



     public override CGRect Frame 
     { 
      get 
      { 
       return base.Frame; 
      } 

      set 
      { 
       value.Y += 4; 
       value.Height -= 2 * 4; 
       base.Frame = value; 
      } 
     } 
    } 

SourceClass:

public class CaseHistorySourceClass : UITableViewSource 
    { 
     private List<CaseSearchItem> caseSearchItems; 
     public CaseSearchItem caseSearchItem; 

    public CaseHistorySourceClass(List<CaseSearchItem> caseSearchItems) 
    { 
     this.caseSearchItems = caseSearchItems; 
    } 
    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) 
    { 
     CaseHistoryTableCell cell = tableView.DequeueReusableCell(CaseHistoryTableCell.Key) as CaseHistoryTableCell ?? CaseHistoryTableCell.Create(); 
     var item = caseSearchItems[indexPath.Row]; 

     cell.BindData(item.Organization, item.Address, item.Doctor, item.UserName); 

     cell.Layer.MasksToBounds = false; 
     cell.Layer.CornerRadius = 10.0f; 
     cell.BackgroundColor = UIColor.White; 



     return cell; 
    } 

    public override nint RowsInSection(UITableView tableview, nint section) 
    { 
     return caseSearchItems.Count; 
    } 


} 

我是新來的iOS和Xamarin。任何幫助被讚賞。

+0

對於自定義單元格u渲染一個'TableCell'的權利,當點擊我假設我應該在ur表格單元格中調用'button clicked'方法。試着把一個斷點,看看它應該點擊它時應該調用視圖,這擴展了TabbleCell – Smit

回答

0

試試這個:

this.btn_viewDetail.TouchUpInside += (sender, e) => 
    { 
    TargetController myTarget = new TargetController(); 
    myTarget.YourString = "Your value here"; 
    this.NavigationController.PushViewController(myTarget, true); 
    }; 
+0

我想'按鈕'上點擊不是'RowClick'。 – Ironman

0

在您的視圖控制器,在那裏你定義你的數據源泰伯維,添加變量,例如您的視圖控制器..

public class mytableDataSource : UITableViewSource 
    { 
     private ViewController owner; 
     List<string> tableItems; 
     public mytableDataSource(ViewController cntrlr, List<string items) 
     { 
      tableItems = items; 
      owner = cntrlr; 

     } 

     public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) 
     { 
      var cell = tableView.DequeueReusableCell("your identifier"); 
      //cell. BindData(..... 
      cell.getDetailButton.Tag = indexPath.Row; 
      cell.getDetailButton.TouchUpInside -= handler; 
      cell.getDetailButton.TouchUpInside += handler; 

     } 
     void handler(Object sender, EventArgs args) 
     { 
      nint tag = btn.Tag; 
      TargetController myTarget = new TargetController(); 
      myTarget.YourString = list[]; 
      owner.NavigationController.PushViewController(myTarget, true); 
     } 

    } 

指定數據源到你的tableview時,例如:

tbl.Source = new mytableDataSource(lstService, this); 
    tbl.ReloadData(); 

要使視圖控制器中的單元格按鈕可用,請在cs文件中創建Property。

public UIButton getDetailButton 
    { 
    get 
     { 
      return btn_viewDetail; 
     } 
    } 
+0

等我試試這個,稍後再告訴你。 – Ironman

+0

@Ironman我已經更新了使用處理程序的答案,以避免在多次調用單元格時在按鈕上獲取多個委託。 – Darshana

+0

我無法在'GetCell'方法中訪問'this.btn_viewDetail.Tag = indexPath.Row;'? – Ironman

相關問題