【问题标题】:Custom TableCell Button click to new ViewController in iOS(Xamarin)自定义 TableCell 按钮单击到 iOS(Xamarin)中的新 ViewController
【发布时间】:2016-11-17 06:46:54
【问题描述】:

我已经为我的 TableView 创建了自定义 TableCell。在自定义单元格中,我有Button。所以我想在点击按钮时打开新的 ViewController。然后在上面发送数据。那么如何在自定义 TableCell 中创建事件以打开新的 ViewController。

我是这样创造的,这样对吗?

我的自定义单元:

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

源类:

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 的新手。感谢您的帮助。

【问题讨论】:

  • 对于自定义单元格,您正在呈现TableCell 正确,当单击时我假设我应该在您的表格单元格中调用button clicked 方法。尝试放置一个断点并查看何时单击它应该调用扩展 TabbleCell 的视图

标签: ios uitableview xamarin xamarin.ios


【解决方案1】:

试试这个:

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

【讨论】:

  • 我想在Button 上点击而不是在RowClick 上。
【解决方案2】:

在您为 Tableview 定义数据源的视图控制器中,例如为您的视图控制器添加变量..

 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)
        { 
            var btn = (Button) sender;
            nint tag = btn.Tag;
            TargetController myTarget = new TargetController();
            myTarget.YourString = list[];
            owner.NavigationController.PushViewController(myTarget, true);
        }
         
    }

将数据源分配给您的表格视图时,也将this 作为参数传递。例如:

 tbl.Source = new mytableDataSource(lstService, this);
  tbl.ReloadData();

要使单元格按钮在视图控制器中可用,请在 cs 文件中创建属性。

public UIButton getDetailButton
    {
    get
        {
            return btn_viewDetail;
        }
    }

【讨论】:

  • 等我试试这个,稍后告诉你。
  • @Ironman 我已经更新了使用处理程序的答案,以避免在多次调用单元格时在按钮上分配多个委托。
  • 我无法在GetCell方法中访问this.btn_viewDetail.Tag = indexPath.Row;
  • 哎呀对不起..try cell.btn_viewDetail.Tag = indexPath.Row ;
  • 如此简单!感谢您帮助我们的菜鸟。
猜你喜欢
  • 1970-01-01
  • 2020-07-21
  • 2018-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-21
相关资源
最近更新 更多