public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            _dataSource = SelectionMenuDataSourceFactory.CreateDataSource(TableView, this, (tableView, item) =>
                                                                          (SelectionTableViewCell)tableView.DequeueReusableCell(nameof(SelectionTableViewCell)));

            _dataSource.Items = _items;

            _dataSource.SelectItem(_items[0]);

            TableView.DataSource      = _dataSource;
            TableView.TableFooterView = new UIView();


            var searchController = new UISearchController((UIViewController)null);

            searchController.ObscuresBackgroundDuringPresentation = false;
            searchController.SearchBar.Placeholder                = "Search...";
            searchController.SearchBar.AutocapitalizationType     = UITextAutocapitalizationType.None;
            searchController.HidesNavigationBarDuringPresentation = false;
            searchController.SearchBar.TextChanged               += (sender, args) =>
            {
                if (args.SearchText.Length >= 2)
                {
                    var query = args.SearchText.ToLowerInvariant();
                    _dataSource.ApplyFilter(i => i.Name.ToLowerInvariant().Contains(query));
                }
                else
                {
                    _dataSource.ClearFilter();
                }
            };

            DefinesPresentationContext = true;


            NavigationItem.LeftBarButtonItem = new UIBarButtonItem("Close", UIBarButtonItemStyle.Plain,
                                                                   (sender, args) => SetResult(Array.Empty <SampleItem>()));

            NavigationItem.RightBarButtonItem = new UIBarButtonItem(UIBarButtonSystemItem.Done,
                                                                    (sender, args) => SetResult(_dataSource.SelectedItems));

            NavigationItem.SearchController = searchController;
        }
示例#2
0
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            // NavigationController.NavigationBar.BackgroundColor = ColorHelper.FromType(ColorType.Navigationbar);
            // NavigationController.NavigationBar.BarTintColor = ColorHelper.FromType(ColorType.Navigationbar);
            // NavigationController.NavigationBar.TintColor = ColorHelper.FromType(ColorType.NavigationbarTint);
            // NavigationController.NavigationBar.TitleTextAttributes = new UIStringAttributes()
            // {
            //     ForegroundColor = ColorHelper.FromType(ColorType.NavigationbarLabel)
            // };

            _tableView = new UITableView
            {
                TranslatesAutoresizingMaskIntoConstraints = false
            };
            View?.AddSubview(_tableView);
            _tableView.RegisterClassForCellReuse(typeof(SelectionTableViewCell2), nameof(SelectionTableViewCell2));


            NSLayoutConstraint.ActivateConstraints(new[]
            {
                _tableView.LeadingAnchor.ConstraintEqualTo(View.LeadingAnchor),
                _tableView.TrailingAnchor.ConstraintEqualTo(View.TrailingAnchor),
                _tableView.BottomAnchor.ConstraintEqualTo(View.BottomAnchor),
                _tableView.TopAnchor.ConstraintEqualTo(View.TopAnchor)
            });


            _dataSource = SelectionMenuDataSourceFactory.CreateDataSource(_tableView, this, (tableView, item) =>
                                                                          (SelectionTableViewCell2)tableView.DequeueReusableCell(nameof(SelectionTableViewCell2)));

            _dataSource.Items = _items;
            if (AllowMultipleSelection && _itemsSelected != null)
            {
                _dataSource.SelectItems(_itemsSelected);
            }
            else if (_itemsSelected != null)
            {
                _dataSource.SelectItem(_itemsSelected[0]);
            }

            _tableView.DataSource = _dataSource;

            _tableView.TableFooterView = new UIView();

            var searchController = new UISearchController((UIViewController)null);


            NavigationItem.LeftBarButtonItem = new UIBarButtonItem(UIBarButtonSystemItem.Close,
                                                                   (sender, args) => SetResult(Array.Empty <SampleItem>()));

            NavigationItem.RightBarButtonItem = new UIBarButtonItem(UIBarButtonSystemItem.Done,
                                                                    (sender, args) => SetResult(_dataSource.SelectedItems));

            if (AllowFiltering)
            {
                searchController.ObscuresBackgroundDuringPresentation = false;
                searchController.HidesNavigationBarDuringPresentation = false;
                if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
                {
                    searchController.AutomaticallyShowsCancelButton = false;
                }

                searchController.SearchBar.Placeholder            = "Search ...";
                searchController.SearchBar.AutocapitalizationType = UITextAutocapitalizationType.None;
                searchController.SearchBar.TextChanged           += (sender, args) =>
                {
                    if (args.SearchText.Length >= 1)
                    {
                        var query = args.SearchText.ToLowerInvariant();
                        _dataSource.ApplyFilter(i => i.Name.ToLowerInvariant().Contains(query));
                    }
                    else
                    {
                        _dataSource.ClearFilter();
                    }
                };

                DefinesPresentationContext = true;

                NavigationItem.SearchController = searchController;
            }

            NavigationItem.HidesSearchBarWhenScrolling = false;
        }