使用 UISearchDisplayController 作为超类
我花了很多精力来创建一个带有自定义单元格的可靠 UITableViewController。现在我想创建一个单独的 UITabbarItem,它在 UISearchDisplayController 中使用 UITableViewController。
遵循 OO 设计原则,我想在定义 UISearchDisplayController 时我会继承原始 UITableViewController 的子类。
例如
@interface SearchViewController : CustomTableViewController
{
NSArray *listContent; // The master content.
NSMutableArray *filteredListContent; // The content filtered as a result of a search.
// The saved state of the search UI if a memory warning removed the view.
NSString *savedSearchTerm;
NSInteger savedScopeButtonIndex;
BOOL searchWasActive;
}
,但是这种方法根本不起作用 - SearchViewController 中的单元格根本没有更新,并且 UITableView 委托方法似乎没有效果(例如,行没有调整大小)。
所以我有几个问题:
这是解决这个问题的正确方法吗?如果是,我如何从超级视图更新 listContent 和filteredListContent。
仅将 UISearchBar 添加到原始搜索视图并根据需要隐藏它会更好吗?
I've put a lot of effort into creating a solid UITableViewController with custom cells. Now I want to create a separate UITabbarItem that uses that UITableViewController within a UISearchDisplayController.
Adhering to OO design principles, I imagine that when defining the UISearchDisplayController I'd subclass the original UITableViewController.
e.g.
@interface SearchViewController : CustomTableViewController
{
NSArray *listContent; // The master content.
NSMutableArray *filteredListContent; // The content filtered as a result of a search.
// The saved state of the search UI if a memory warning removed the view.
NSString *savedSearchTerm;
NSInteger savedScopeButtonIndex;
BOOL searchWasActive;
}
However this approach doesn't work at all - the cells are not updated at all in SearchViewController, and the UITableView delegate methods do not seem to have an effect (e.g. rows are not resized).
So I have several questions:
Is this the correct way to go about this, if so, how do I update the listContent and the filteredListContent from the superview.
Would it be better to just add a UISearchBar to the original search view and hide it as necessary?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不认为你可以继承 UISearchDisplayController 并让它正常工作。它在非公开的方法中做了很多事情,所以你无法用正确的行为覆盖它们。
但是,您可以按原样将内置的 UISearchDisplayController 与搜索结果中的自定义表格单元格一起使用。您需要封装自定义单元格的创建和配置,以便只需覆盖 -...cellForRowAtIndexPath (这是在表视图中显示自定义数据的标准方法)即可在任何表视图中工作。确保该控制器是 UISearchDisplayDelegate,它将使用该方法在搜索列表中创建行。
要设置自定义高度,请
在 searchTableView 上设置 rowHeight。
I don't think you can subclass UISearchDisplayController and have it work correctly. It does a lot of stuff in methods which aren't public, so you wouldn't be able to override them with the correct behavior.
You can, however, use the built in UISearchDisplayController with your custom table cells in the search results, as is. You need to encapsulate the creation and configuring of your custom cells such that it works in any table view just by overriding -...cellForRowAtIndexPath (this is the standard method of displaying custom data in a tableview). Make sure that controller is the UISearchDisplayDelegate and it'll use that method to create the rows in your search list.
To set the custom height, implement
to set the rowHeight on the searchTableView.