【问题标题】:Update textfield after a row is selected in tableview Xamarin.IOS在 tableview Xamarin.IOS 中选择一行后更新文本字段
【发布时间】:2016-08-01 15:12:29
【问题描述】:

这是我遇到的一个场景。我有一个用户名作为字段之一的登录屏幕。用户可以使用下拉列表选择用户(通过按钮实现 - 模拟下拉箭头、弹出控制器和表格视图)。我有一个控制器(UserNameController),它具有获取用户名并将其绑定到其中的表视图的逻辑。 UserNameController 是通过 ViewController.cs 调用的,它具有文本字段和下拉按钮,使用以下代码:

var content = this.Storyboard.InstantiateViewController("UserNameLookUp") as UserNameController;
            UIPopoverController popover = new UIPopoverController(content);

            //popover.SetPopoverContentSize(new SizeF(80, 80), true);

            popover.PresentFromRect(new RectangleF(float.Parse((sender.Frame.X + 115).ToString()),
                                                   float.Parse((sender.Frame.Y + 180).ToString())
                                                   , 80, 80), View, UIPopoverArrowDirection.Up, true);

在用户名控制器中:

public override void ViewDidLoad()
        {
            string[] userName = new string[10];

                tblVwUserName.Source = new TableSource(userName);
            }
        }

TabelSource.cs 看起来像这样:

public class TableSource : UITableViewSource
    {
        string[] TableItems;
        string CellIdentifier = "TableCell";

        public TableSource(string[] items)
        {
            TableItems = items;
        }

        public override nint RowsInSection(UITableView tableview, nint section)
        {
            return TableItems.Length;
        }

        public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
        {
            UITableViewCell cell = tableView.DequeueReusableCell(CellIdentifier);
            string item = TableItems[indexPath.Row];

            //---- if there are no cells to reuse, create a new one
            if (cell == null)
            { cell = new UITableViewCell(UITableViewCellStyle.Default, CellIdentifier); }

            cell.TextLabel.Text = item;

            return cell;
        }

        public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
        {
            // HOW TO SET BACK THE USERNAME TEXT FIELD IN VIEWCONTROLLER? AND CLOSE THE POPUP
            tableView.DeselectRow(indexPath, true);
        }
    }

现在我需要如何在视图控制器的文本字段中显示选定的用户名并关闭弹出窗口?

谢谢! 席德

【问题讨论】:

    标签: uitableview xamarin xamarin.ios uipopovercontroller


    【解决方案1】:

    我通常使用 TableViewSource 和 TableViewController 上的属性来解决这个要求。您的来源将更改为:

    public class TableSource : UITableViewSource
    {
        string[] TableItems;
        string CellIdentifier = "TableCell";
    
        public string SelectedItem {get; set;}
    
        public TableSource(string[] items)
        {
            TableItems = items;
        }
    
        public override nint RowsInSection(UITableView tableview, nint section)
        {
            return TableItems.Length;
        }
    
        public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
        {
            UITableViewCell cell = tableView.DequeueReusableCell(CellIdentifier);
            string item = TableItems[indexPath.Row];
    
            //---- if there are no cells to reuse, create a new one
            if (cell == null)
            { cell = new UITableViewCell(UITableViewCellStyle.Default, CellIdentifier); }
    
            cell.TextLabel.Text = item;
    
            return cell;
        }
    
        public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
        {
            SelectedItem = items[indexPath.Row];            
    
            tableView.DeselectRow(indexPath, true);
        }
    }
    

    现在您需要一种方法来获得该值。有两种可能的方法:

    1) UserNameController 的类型是 UITableViewController,而不是 (content.TableView.Source as TableSource).SelectedItem

    2) UserNameController 不是 UITableViewController 类型,我也会添加一个属性来返回 TableViewSource 的属性。

    最后要做的是关闭 Popover 并查询所选项目。对于这个任务,我会将 UserNameController 包装在 UINavigationController 中,并在其中添加一个 Cancel 和一个 Done 按钮,如下所示:

     var navigationController = new UINavigationController(content);
     var popover = new UIPopoverController(navigationController);
    
     content.NavigationItem.SetLeftBarButtonItem(new UIBarButtonItem(UIBarButtonSystemItem.Cancel, (s, e) =>
     {
         parentController.DismissViewController(true, null);
     }), true);
     content.NavigationItem.SetRightBarButtonItem(new UIBarButtonItem(UIBarButtonSystemItem.Done, (s, e) =>
     {
         parentController.DismissViewController(true, null);
    
         var selectedItem = (content.Source as TableSource).SelectedItem;
      }), true);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多