iOS(Xamarin) 中 TableView 中单个单元格中的两个按钮单击事件

Two Button click event in single cell in TableView in iOS(Xamarin)

我正在 xamarin 中制作自定义单元格 iOS。在单元格中,我有两个按钮,如下图所示。

图:

两个按钮是:

  1. 创建约会
  2. 查看详情

我想在我的源文件中创建这两个不同的按钮单击事件 class 以便我可以将数据发送到不同的 ViewController 以达到我的目的。

代码: 表格单元格 class :

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

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

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

        public static CaseHistoryTableCell Create()
        {
            return (CaseHistoryTableCell)Nib.Instantiate(null, null)[0];
        }

        public 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_caseDate.TextColor = UIColor.Clear.FromHexString("#000000", 0.54f);
            this.lbl_scheDate.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);
        }

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

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

    }

来源 Class :

public class CaseHistorySourceClass : UITableViewSource
        {
            private List<CaseSearchItem> caseSearchItems;
            public CaseSearchItem caseSearchItem;
            public static event EventHandler RowClicked;

            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;
                cell.SetNeedsLayout();
                cell.LayoutIfNeeded();
                return cell;
            }


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

我的问题:

可以在一个 Cell 中创建两个不同的 Button 单击事件。

如果是那么如何?

如果不是,那么执行此类操作的替代方法是什么。

Note : I doesn't want to require RowSelected. I only require how to perform this two different Button click Event.

不要在RowSelected上做这样的操作 使用选择器设置目标将帮助您。

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

// setTag to button to identify in which row button is pressed 
cell.btnCreateAppointment.tag=indexPath.Row;
cell.btnViewDetail.tag=indexPath.row;

// set Target to a method 
cell.btnCreateAppointment.TouchUpInside += createAppointment;
ell.btnViewDetail.TouchUpInside +=viewDetail;


            }

按下按钮时将调用这些方法

 public void createAppointment(object sender, EventArgs e)
{
 var row=sender.tag;

}

第二个按钮点击事件

public void viewDetail(object sender, EventArgs e)
{
 var row=sender.tag;

}

我希望这个工作。

您可以通过从 GetCell 方法向按钮添加目标操作来完成此操作:

首先在您的单元格中添加以下内容 class 以使按钮易于访问:

public UIButton btnCreateAppointment { 

            get
            {
                return this.btn_createAppointment;
            }
        }


        public UIButton btnViewDetail 
        {

            get
            {
                return this.btn_viewDetail;
            }
        }

现在修改您的 GetCell 方法以添加操作目标

cell.btnCreateAppointment.tag = indexPath.Row;
cell.btnViewDetail.tag = indexPath.row;


//assign action
cell.btnCreateAppointment.TouchUpInside += (sender, e) =>
            {
                 var row = ((UIButton)sender).Tag;
                 var item = caseSearchItems[row];

            };
 cell.btnViewDetail.TouchUpInside += (sender, e) =>
            {
                 var row = ((UIButton)sender).Tag;
                 var item = caseSearchItems[row];

            };