user2697452
user2697452

Reputation: 352

Update textfield after a row is selected in tableview Xamarin.IOS

Here is a scenario that I am encountering. I am having a login screen with UserName as one of the fields. The user can select a user using a dropdown list (implemented via a button - to simulate drop down arrow, a popover controller and a table view). I having a controller (UserNameController) which has the logic of fetching the usernames and binding the same to the table view within it. The UserNameController is called via ViewController.cs which has the textfield and the dropdown button using below code:

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

and in UserNameController:

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

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

and TabelSource.cs looks like this:

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

Now how do i need to display the selected username in the viewcontroller's textfield and close the popup?

Thanks! Sid

Upvotes: 0

Views: 752

Answers (1)

tequila slammer
tequila slammer

Reputation: 2879

I typically solve this requirement with properties on the TableViewSource and the TableViewController. Your source will change to:

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

Now you need a way to get that value. There are two possible ways:

1) UserNameController is of type UITableViewController, than it would be (content.TableView.Source as TableSource).SelectedItem

2) UserNameController is not of type UITableViewController, than I would add a property too that returns the property of the TableViewSource.

Last thing to do is closing the Popover and query the selected item. For this task I would wrap the UserNameController in a UINavigationController and add a Cancel and a Done button into it like this:

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

Upvotes: 0

Related Questions