- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
所以我目前正在收听来自 searchBar 的文本更改:
-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)searchText
{
[self filterContentForSearchText:searchText];
}
我想设计一个方法 filterContentForsearchText
以便它在我键入时自动过滤我的 UITableView。
我遇到的问题是我的 UITableView 很复杂。它有多个部分和多个字段。它基本上是一个联系人/地址簿,是一个包含联系人对象的数组。
CustomContact *con = [contacts[section_of_contact] allValues][0][x]
其中 x 是该部分中的特定行返回具有 con 等属性的 CustomContact“con” .fullName
.
UITableView 目前在单独的部分中显示联系人的全名。我如何在使用 UISearchBar
键入时过滤数组/UITableView 的这种结构?
表格的填充方式如下:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
BrowseContactCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BROWSECELL"];
CustomContact *thisContact = [self.contacts[indexPath.section] allValues][0][indexPath.row];
cell.labelName.text = thisContact.fullName;
return cell;
}
最佳答案
我还创建了一个包含部分(数组的数组)的地址簿,所以我只发布我的解决方案,但这不是我自己的解决方案(我前段时间在 stackoverflow 的某个地方找到它):
在您的 UITableViewController
子类中,只需添加以下两个 UISearchDisplayDelegate
方法 ( link to the api reference ) 和“自定义”方法 filterContentForSearchText
进行过滤你的阵列。为了更好地理解,请阅读我在代码块中的评论。欢迎提出更多问题或提出改进意见。
#pragma mark - search Display Controller Delegate
- (BOOL) searchDisplayController : (UISearchDisplayController *) controller
shouldReloadTableForSearchString : (NSString *) searchString {
[self filterContentForSearchText : searchString
scope : [[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex : [self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
return YES;
}
#pragma mark - Search Filter
- (void) filterContentForSearchText : (NSString*) searchText
scope : (NSString*) scope {
// Here, instead of "lastName" or "firstName" just type your "fullName" a.s.o.
// I have also a custom NSObject subclass like your CustomContact that holds
//these strings like con.firstName or con.lastName
NSPredicate* resultPredicate = [NSPredicate predicateWithFormat : @" (lastName beginswith %@) OR (firstName beginsWith %@)", searchText, searchText];
// For this method, you just don't need to take your partitioned and
// somehow complicated contacts array (the array of arrays).
// Instead, just take an array that holds all your CustomContacts (unsorted):
NSArray* contactsArray = // here, get your unsorted contacts array from your data base or somewhere else you have stored your contacts;
// _searchResults is a private NSArray property, declared in this .m-file
// you will need this later (see below) in two of your UITableViewDataSource-methods:
_searchResults = [contactsArray filteredArrayUsingPredicate : resultPredicate];
// this is just for testing, if your search-Filter is working properly.
// Just remove it if you don't need it anymore
if (_searchResults.count == 0) {
NSLog(@" -> No Results (If that is wrong: try using simulator keyboard for testing!)");
} else {
NSLog(@" -> Number of search Results: %d", searchResults.count);
}
}
现在,您需要对以下三个 UITableViewDataSource
方法进行一些更改:
注意:searchResultsTableView也加载了普通的公共(public)数据源通常调用的方法来填充您的(分段地址簿)tableView:
1.
- (NSInteger) numberOfSectionsInTableView : (UITableView *) tableView
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
// in our searchTableView we don't need to show the sections with headers
return 1;
}
else {
return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] count];
}
}
2.
- (NSInteger) tableView : (UITableView *) tableView
numberOfRowsInSection : (NSInteger) section {
if (tableView == self.searchDisplayController.searchResultsTableView) {
// return number of search results
return [_searchResults count];
} else {
// return count of the array that belongs to that section
// here, put (or just let it there like before) your
// [contacts[section] allValues][count]
return [[currentMNFaces objectAtIndex : section] count];
}
}
3.
- (UITableViewCell*) tableView : (UITableView *) tableView
cellForRowAtIndexPath : (NSIndexPath *) indexPath {
BrowseContactCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BROWSECELL"];
CustomContact *thisContact = nil;
// after loading your (custom) UITableViewCell
// you probably might load your object that will be put into the
// labels of that tableCell.
// This is the point, where you need to take care if this is your
// address-book tableView that is loaded, or your searchTable:
// load object...
if (tableView == self.searchDisplayController.searchResultsTableView) {
// from search list
faceAtIndex = [self->searchResults objectAtIndex: indexPath.row];
} else {
// from your contacts list
contactAtIndex = [self.contacts[indexPath.section] allValues][0][indexPath.row];
}
}
希望对你有用。
关于ios - 在具有多个部分的 UITableView 中从 UISearchBar 按类型搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17032263/
这个问题本质上是,UISearchBar在激活时与状态栏重叠。我知道已经有很多类似的问题发布了,我已经尝试了所有方法,但仍然无法解决。 当屏幕初始化时,搜索栏似乎位于正确的位置。当我向下滚动屏幕时,您
我尝试在我的应用程序中实现 iOS 11 中的新搜索栏,但出于某种原因,我无法让搜索栏颜色随导航栏一起更改。在切换到新样式之前,我在 AppDelagate 中使用 UISearchBar.appea
我正在努力做到这一点: Consider a table view with Search Bar on its header.Table view gets refreshed whenever s
documentation表示 UISearchBar我们可以在 tvOS 中使用它(Apple 当然在他们的搜索 View 中使用它),但我在对象库中找不到它。 其他人有同样的问题吗?我没有看到一个
当我在 UISearchBar 上搜索时,我的普通 tableView 已设置为 - (UITableViewCell *)tableView:(UITableView *)tableView cel
这里是我在view controller.h中的代码 #import "AppDelegate.h" @interface SearchRsultsFanSideViewController : UI
我在一个应用程序工作,我今天在 iOS 7.1 上进行了测试。到目前为止,我的搜索栏是正常的: 但现在我有这个问题: 出现一个灰色 View ,我不知道如何删除它,因为我的代码中没有这个 View 。
我正在更新适用于 iOS 7 的应用程序,而我的一项功能(允许在搜索栏中没有文本的情况下激活搜索栏搜索按钮)停止工作。我使用了以下代码。关于如何让它再次工作有什么建议吗?提前致谢。 UITextFie
我有一个带有 UISearchBar(和 SearchDisplayController)的 UIViewController 以及一个 UITableView。当导航到这个 View Control
我有一个我似乎无法克服的小情况。 我有一个 UITableView,它的顶部嵌入了一个 UISearchBar。 TableView 由 NSFetchedResultsController 和 Co
我在 TableView 顶部使用带有 UISearchBar 的 UITableView,对我来说,一切都工作正常,只有一个问题,比如如果我搜索一个字母,uitableview 中的搜索结果被过滤,
表格标题中有一个搜索栏。当用户在 iOS 7 上快速点击它两次时,它就会消失。有人对我们做错了什么有什么建议吗? 最佳答案 经过大量的试验和错误,我发现当searchDisplayController
我想更改 UISearchBar。文本字段的高度和宽度。我的问题是如何更改 iphone 中 UISearchBar 中的 UiSearchbar 高度、宽度、颜色 和 Uitextfield 高度?
这两张图应该可以说明了吧。第一个正常显示我的 UITableView。第二个显示当我触摸 UISearchBar 并且它获得焦点时。为什么单元格标题和搜索栏之间有间隙?有人知道如何解决这个问题吗? i
我有一个 UISearchBar添加到 UITableView 的顶部. // init search bar self.resultSearchController = ({
在 iOS 5 上使用带有 showCancelButton=YES 的 UISearchBar。希望在键盘下拉时取消按钮保持启用状态。使用以下代码似乎不起作用: for (id subView in
我有一个 UITableView,它的数据源和委托(delegate)不是由放置它的 View Controller 处理的,而是由另一个名为 AbstractInviteFriendsDataSou
我研究过这个问题。看起来这是一个常见问题,但我尝试过的任何方法都没有奏效。我对 iPhone 应用程序开发和 objective-c 非常陌生。此时我想做的就是在搜索栏字段中输入一个字符串,并在点击键
我有一个使用 UISearchBar 的搜索功能,该功能是即时发生的,所以我认为用“完成”替换键盘上的“搜索”按钮会更明显。 有办法做到这一点吗? 谢谢 最佳答案 您可以更改 UISearchBar
有没有人设法为 UISearchBar 的范围栏部分着色,它有一个 tintColor 属性,但设置它不会影响附加的范围栏 UISegmentedControl。我在酒吧上有一个 Handlebars
我是一名优秀的程序员,十分优秀!