简体   繁体   English

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

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

I am making custom cell in xamarin iOS. 我在xamarin iOS中制作自定义单元。 In the cell I have two Button which is display in the below figure. 在单元格中,有两个Button,如下图所示。

Figure : 图:

在此处输入图片说明

Two Button are : 两个按钮是:

  1. Create Appointment 创建约会
  2. View Detail 查看详细

I want to create this two different Button click Event in My Source class so that I can send data to different ViewController for my purpose. 我想在My Source类中创建这两个不同的Button click Event,以便可以将数据发送到不同的ViewController以达到我的目的。

Code : TableCell class : 代码: TableCell类:

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

    }

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

My Question : 我的问题 :

It is possible to Create two different Button click event in a single Cell. 可以在单个单元格中创建两个不同的Button单击事件。

If yes then How ? 如果是,那怎么办?

and If No then what is alternative to Perform this type of operation. 如果否,则执行此类型操作的替代方法是什么。

Note : I doesn't want to require RowSelected . 注意:我不想要求RowSelected I only require how to perform this two different Button click Event. 我只需要如何执行这两个不同的Button click事件。

Don't do such operation on RowSelected Setting Target with selector will help you out . 不要对带选择器的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;


            }

These Method will be called when Press you button 按下按钮时将调用这些方法

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

}

Second button Clicked event 第二个按钮的单击事件

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

}

i hope this work. 我希望这项工作。

You can do this by adding target action to button from your GetCell method: 您可以通过从GetCell方法向按钮添加目标操作来做到这一点:

first add following in your cell class to make buttons accessible: 首先在您的单元格类中添加以下内容,以使按钮可访问:

public UIButton btnCreateAppointment { 

            get
            {
                return this.btn_createAppointment;
            }
        }


        public UIButton btnViewDetail 
        {

            get
            {
                return this.btn_viewDetail;
            }
        }

Now from modify your GetCell method to add action target 现在,从修改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];

            };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM