gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 19:33:48 25 4
gpt4 key购买 nike

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

图:

enter image description here

两个按钮是:

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

我想在我的源类中创建这两个不同的按钮单击事件,以便我可以将数据发送到不同的 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;
}
}

我的问题:

可以在一个单元格中创建两个不同的按钮点击事件。

如果是那么如何?

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

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;

}

我希望这能奏效。

关于ios - iOS 中 TableView 中单个单元格中的两个按钮单击事件(Xamarin),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41355434/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com