ios - 在具有多个部分的 UITableView 中从 UISearchBar 中搜索您的类型?
<p><p>所以我目前正在收听来自 searchBar 的文本更改:</p>
<pre><code>-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)searchText
{
;
}
</code></pre>
<p>我想设计一个方法 <code>filterContentForsearchText</code> 以便在我键入时自动过滤我的 UITableView。</p>
<p>我遇到的问题是我的 UITableView 很复杂。它有多个部分和多个字段。它基本上是一个联系人/地址簿,是一个包含联系人对象的数组。</p>
<p><code>CustomContact *con = allValues]</code> 其中 x 是该部分中的特定行,返回具有 <code>con 等属性的 CustomContact “con” .fullName</code>.</p>
<p>UITableView 当前在单独的部分中显示联系人的全名。当我使用 <code>UISearchBar</code> 键入时,如何过滤数组/UITableView 的这种结构?</p>
<p>表格的填充方式如下:</p>
<pre><code>-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
BrowseContactCell *cell = ;
CustomContact *thisContact = allValues];
cell.labelName.text = thisContact.fullName;
return cell;
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我还创建了一个带有部分(数组数组)的地址簿,所以我将发布我的解决方案,但这不是我自己的解决方案(我前段时间在 stackoverflow 上的某个地方找到了它):</p>
<p>在您的 <code>UITableViewController</code> 子类中,只需添加以下两个 <code>UISearchDisplayDelegate</code> 方法( <a href="https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UISearchDisplayDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intf/UISearchDisplayDelegate" rel="noreferrer noopener nofollow">link to the api reference</a> )和“自定义”方法 <code>filterContentForSearchText</code> 进行过滤你的阵列。为了更好地理解,请阅读我在代码块中的评论。欢迎提出进一步的问题或改进意见。</p>
<pre><code>#pragma mark - search Display Controller Delegate
- (BOOL) searchDisplayController : (UISearchDisplayController *) controller
shouldReloadTableForSearchString : (NSString *) searchString {
[self filterContentForSearchText : searchString
scope : [
objectAtIndex : ]];
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 = ;
// 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 = ;
// 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);
}
}
</code></pre>
<p>现在,您需要对以下三个 <code>UITableViewDataSource</code> 方法进行一些更改:</p>
<p><strong>注意</strong>:searchResultsTableView也加载了普通的普通数据源
通常调用来填充您的(分段地址簿-)tableView 的方法:</p>
<p><strong>1.</strong></p>
<pre><code>- (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 [[ sectionTitles] count];
}
}
</code></pre>
<p><strong>2.</strong></p>
<pre><code>- (NSInteger) tableView : (UITableView *) tableView
numberOfRowsInSection : (NSInteger) section {
if (tableView == self.searchDisplayController.searchResultsTableView) {
// return number of search results
return ;
} else {
// return count of the array that belongs to that section
// here, put (or just let it there like before) your
// allValues]
return [ count];
}
}
</code></pre>
<p><strong>3.</strong> </p>
<pre><code>- (UITableViewCell*) tableView : (UITableView *) tableView
cellForRowAtIndexPath : (NSIndexPath *) indexPath {
BrowseContactCell *cell = ;
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 = ;
} else {
// from your contacts list
contactAtIndex = allValues];
}
}
</code></pre>
<p>希望对你有用。</p></p>
<p style="font-size: 20px;">关于ios - 在具有多个部分的 UITableView 中从 UISearchBar 中搜索您的类型?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/17032263/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/17032263/
</a>
</p>
页:
[1]