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

时间:2016-12-28 04:39:00

标签: ios uitableview xamarin xamarin.ios tableviewcell

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

图:

enter image description here

两个按钮是:

  1. 创建约会
  2. 查看详情
  3. 我想在My Source类中创建这两个不同的Button click事件,以便我可以为我的目的将数据发送到不同的ViewController。

    代码: 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;
                }
            }
    
        }
    

    来源类:

    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 click事件。

    如果是,那么如何?

    如果否,则执行此类操作的替代方法。

      

    注意:我不想要RowSelected。我只需要怎么做   执行这两个不同的按钮单击事件。

2 个答案:

答案 0 :(得分:2)

不要在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;

}

我希望这项工作。

答案 1 :(得分:1)

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

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

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

            };