c# - UISearchController 和 MvvmCross
<p><p>我想为我的应用程序 (IOS8) 添加搜索逻辑。我有简单的 <code>MvxTableViewController</code> 并通过 <code>UITableViewSource</code> 显示我的数据。这是:
... Controller :</p>
<pre><code> 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();
}
}
</code></pre>
<p>我阅读了 IOS 中的搜索并为 IOS8 应用选择了 <code>UISearchController</code>。但我不明白,如何将此 Controller 添加到我的 View 中:(
我从 Xamarin (TableSearch) 中找到了示例 - 但他们不使用 <code>UITableViewSource</code> 我不明白我应该怎么做。
我尝试添加 Controller :</p>
<pre><code>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;
</code></pre>
<p>我应该在 this.searchTableController 中做什么?我需要将我的显示逻辑移到那里吗?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>是的。 “searchTableController”应该负责搜索结果的呈现。</p>
<p> <a href="https://developer.apple.com/library/ios/samplecode/TableSearch_UISearchController/Introduction/Intro.html" rel="noreferrer noopener nofollow">Here is the test project (native, not xmarin) which help you understand.</a> </p>
<p>searchController 管理“searchBar”和“searchResultPresenter”。他不需要添加到运营商 Controller 的 View 层次结构中。当用户开始在“searchBar”中输入文本时,“SearchController”会自动为您显示您的 SearchResultPresenter。</p>
<p>步骤:
1) 使用 SearchResultsPresenterController 实例化搜索 Controller 。 </p>
<p>2) 当用户在搜索栏中输入文本时,您应该调用您自己的搜索服务。下面是代码示例..</p>
<pre><code>#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
;
}
}
</code></pre>
<p>3)在搜索presenter中需要重新加载一个表的方法“dataFound:”</p>
<pre><code>- (void)dataFound:(NSArray *)searchResults
{
_searchResults = searchResults;
;
}
</code></pre></p>
<p style="font-size: 20px;">关于c# - UISearchController 和 MvvmCross,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/31556222/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/31556222/
</a>
</p>
页:
[1]