2016-11-17 9 views
0

私は自分のTableViewのカスタムTableCellを作成しています。カスタムセルでは私はButtonです。ですから、ボタンがクリックされたときに新しいViewControllerを開き、その上でデータを送信したいと思います。それで、新しいViewControllerを開くためにカスタムTableCellでイベントを作成する方法。カスタムTableCellボタンiOS(Xamarin)の新しいViewControllerをクリックしてください

私はこの方法を作成していますこの正しい方法ですか?

私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

カスタムセルでは、「TableCell」権利がレンダリングされています。クリックすると、テーブルセルに「button clicked」メソッドを呼び出す必要があります。ブレークポイントを置いて、クリックするとビューを呼び出す必要があることを確認してください。thatsはTabbleCellを拡張します – Smit

答えて

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); 
     } 

    } 

...例えば、あなたのビューコントローラのための変数を追加し、あなたがあなたのテーブルビューのためのデータソースを定義し、あなたのビューコントローラでは、

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

'Button'をクリックすると' RowClick'が表示されません。 – Ironman

0

tbl.Source = new mytableDataSource(lstService, this); 
    tbl.ReloadData(); 
など、パラメータとして thisも渡します。

View Controllerでセルボタンを使用できるようにするには、csファイルにPropertyを作成します。

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

私はこれを試して後で教えてください。 – Ironman

+0

@アイロンマン私はハンドラを使用して複数の代理人を取得するのを避けるために、ボタンを複数回呼び出すと、 – Darshana

+0

'GetCell'メソッドで' this.btn_viewDetail.Tag = indexPath.Row; 'にアクセスできません。 – Ironman

関連する問題