2015-07-22 35 views
1

我想为我的应用程序(IOS8)添加搜索逻辑。我有简单的MvxTableViewController,并显示我的数据UITableViewSource。这里是: ...控制器:UISearchController和MvvmCross

MvxViewFor(typeof(MainViewModel))] 
    partial class MainController : MvxTableViewController 
    { 
     public MainController(IntPtr handle) : base(handle) { } 


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


      // make background trasnsparent page 
      this.View.BackgroundColor = UIColor.Clear; 
      this.TableView.BackgroundColor = UIColor.Clear; 
      this.NavigationController.NavigationBar.BarStyle = UIBarStyle.Black; 

      this.SetBackground(); 

      (this.DataContext as MainViewModel).PropertyChanged += this.ViewModelPropertyChanged; 
     } 

     private void SetBackground() 
     { 
      // set blured bg image 

     } 

     private void ViewModelPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) 
     { 
      var viewModel = this.ViewModel as MainViewModel; 
      if (e.PropertyName == "Title") 
      { 
       this.Title = viewModel.Title; 
      } 
      else if (e.PropertyName == "Topics") 
      { 
       var tableSource = new TopicTableViewSource(viewModel.Topics); 
       tableSource.TappedCommand = viewModel.NavigateToChildrenPageCommand; 

       this.TableView.Source = tableSource; 
       this.TableView.ReloadData(); 
      } 
     } 

我读到的IOS搜索和选用UISearchController为iOS8上的应用程序。但我不明白,我怎么可以将这个控制器添加到我的观点:( 我发现Xamarin(TableSearch)的示例 - 但他们不使用UITableViewSource,我不明白我应该怎么做这个 我尝试添加控制器:

this.searchController = new UISearchController (this.searchTableController) 
{ 
     WeakDelegate = this, 
     DimsBackgroundDuringPresentation = false, 
     WeakSearchResultsUpdater = this, 
}; 

this.searchController.SearchBar.SizeToFit(); 
this.TableView.TableHeaderView = searchController.SearchBar; 

this.TableView.WeakDelegate = this; 
this.searchController.SearchBar.WeakDelegate = this; 

什么,我应该做this.searchTableController我是否需要将我的显示逻辑有

回答

2

是在“searchTableController”应该是负责搜索结果的呈现

?。

Here is the test project (native, not xmarin) which help you understand.

searchController管理“searchBar”和“searchResultPresenter”。他不需要添加到运营商控制器的视图层次结构中。当用户开始在“searchBar”中输入文本时,“SearchController”会自动为您显示SearchResultPresenter。

步骤: 1)使用SearchResultsPresenterController实例化搜索控制器。

2)当用户在搜索栏中输入文本时,您应该调用您自己的搜索服务。下面的代码示例..

#pragma mark - UISearchResultsUpdating 

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController 
{ 
    NSString *searchString = searchController.searchBar.text; 

    if (searchString.length > 1) 
    { 
     // TODO - call your service for the search by string 
     // this may be async or sync 
     // When a data was found - set it to presenter 
     [self.searchResultPresenter dataFound:<found data>]; 
    } 
} 

3)在搜索演示需要重新加载表的方法“dataFound:”

- (void)dataFound:(NSArray *)searchResults 
{ 
    _searchResults = searchResults; 
    [self.tableView reloadData]; 
} 
+0

谢谢。 我看到了这个示例,它类似于xamarin samle([TableSearch](https://developer.xamarin.com/samples/monotouch/ios8%5CTableSearch/))。 但在这些示例中不使用UITableViewSource。 我不明白,我怎么能结合,我应该如何编辑我的逻辑正确的搜索工作。 –

+0

@NikitaBondarenko加了解释 –

+0

我明白了。我需要创建新的'TableViewController'来显示搜索结果。这应该是简单的控制器与项目列表和覆盖显示默认方法('RowsIsSection'和'GetCell'等),或者你自己'UITableViewSource'与更新此源的方法。 谢谢。 –

1

下面是关于如何使用UISearchController一些建议Xamarin.iOS。

  1. 对结果表视图子类UITableViewSource创建一个新的。这将是负责显示结果的视图。您需要制作该表格视图的项目列表public

    public List<string> SearchedItems { get; set; } 
    
  2. 在你的主UIViewController,创建UISearchController,并通过你的结果表视图作为参数。我添加了一些额外的设置。

    public UISearchController SearchController { get; set; } 
    
    public override void ViewDidLoad() 
    { 
        SearchController = new UISearchController (resultsTableController) { 
         WeakDelegate = this, 
         DimsBackgroundDuringPresentation = false, 
         WeakSearchResultsUpdater = this, 
        }; 
    
        SearchController.SearchBar.SizeToFit(); 
        SearchController.SearchBar.WeakDelegate = this; 
        SearchController.HidesNavigationBarDuringPresentation = false; 
    
        DefinesPresentationContext = true; 
    } 
    
  3. 搜索栏添加到您的UI在用户体验来看,在我看来,最好的办法,是把它作为一个NavigationItem添加到NavigationBarController

    NavigationItem.TitleView = SearchController.SearchBar; 
    
  4. 添加方法,在主UIViewController执行搜索:

    [Export ("updateSearchResultsForSearchController:")] 
    public virtual void UpdateSearchResultsForSearchController (UISearchController searchController) 
    { 
        var tableController = (UITableViewController)searchController.SearchResultsController; 
        var resultsSource = (ResultsTableSource)tableController.TableView.Source; 
        resultsSource.SearchedItems = PerformSearch (searchController.SearchBar.Text); 
        tableController.TableView.ReloadData(); 
    } 
    
    static List<string> PerformSearch (string searchString) 
    { 
        // Return a list of elements that correspond to the search or 
        // parse an existing list. 
    } 
    

我真的希望这会帮助你,祝你好运。

+0

我明白了。我需要创建新的'TableViewController'来显示搜索结果。这应该是简单的控制器与项目列表和覆盖显示默认方法('RowsIsSection'和'GetCell'等),或者你自己'UITableViewSource'与更新此源的方法。 谢谢。 –

+0

这是正确的。 注意:'UITableViewSource'只是一个结合了'UITableViewDataSource'和'UITableViewDelegate'的帮手。 –