Ios 搜索显示控制器结果仅来自my UITableView的第一部分
我有一个带搜索显示控制器的分区UITableView。我的问题是,在搜索过程中,结果中只显示表第一部分的行 我的代码如下: 首先在ViewDidLoad中,我获得了部分,并创建了搜索结果数组,其容量等于我的db表中记录的总数Ios 搜索显示控制器结果仅来自my UITableView的第一部分,ios,uitableview,uisearchbar,Ios,Uitableview,Uisearchbar,我有一个带搜索显示控制器的分区UITableView。我的问题是,在搜索过程中,结果中只显示表第一部分的行 我的代码如下: 首先在ViewDidLoad中,我获得了部分,并创建了搜索结果数组,其容量等于我的db表中记录的总数 - (void)viewDidLoad { [super viewDidLoad]; FMDatabase *db = [FMDatabase databaseWithPath:[Utility getDatabasePath]]; [db open]; //L
- (void)viewDidLoad
{
[super viewDidLoad];
FMDatabase *db = [FMDatabase databaseWithPath:[Utility getDatabasePath]];
[db open];
//Loading the sections for the TableView
self.sectionsarray =[[NSMutableArray alloc] init];
FMResults *results = [db executeQuery:@"SELECT distinct section FROM articles where order by section"];
while([results next])
{
NSString *sectioname = [results stringForColumn:@"section"];
[sectionsarray addObject:sectioname];
}
[db close];
//Creating the Array for the search results
self.searchResults = [NSMutableArray arrayWithCapacity:[db intForQuery: [NSString stringWithFormat:@"SELECT count(id) as rowscount FROM Articles"]]];
if (self.savedSearchTerm)
{
[self.searchDisplayController.searchBar setText:savedSearchTerm];
self.savedSearchTerm = nil;
}
[self.tableView reloadData];
}
下面的代码负责执行实际的搜索。它在保存UITableView数据的数组中名为ArticleTags的记录的任何部分中查找搜索项
- (void)handleSearchForTerm:(NSString *)searchTerm
{
// Update the filtered array based on the search text
[self.searchResults removeAllObjects];
for (Article *searchplace in masterplacesarray)
{
NSRange range = [searchplace.articletags rangeOfString:searchTerm options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
if (range.location != NSNotFound) {
[self.searchResults addObject:searchplace];
}
}
}
下面将计算表视图中的节数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableView == [[self searchDisplayController] searchResultsTableView])
return 1;
else {
return [sectionsarray count];
}
}
这是每个部分的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == [[self searchDisplayController] searchResultsTableView])
return [[self searchResults] count];
else
{
FMDatabase *db = [FMDatabase databaseWithPath:[Utility getDatabasePath]];
[db open];
NSUInteger rowscountforsection = [db intForQuery: [NSString stringWithFormat:@"SELECT count(id) as rowscount FROM Articles where section='%@'",[self.sectionsarray objectAtIndex:section]]];
[db close];
return rowscountforsection;
}
}
这将设置tableview每个部分的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if (tableView == [[self searchDisplayController] searchResultsTableView])
return @"Search Results";
else
return [self.sectionsarray objectAtIndex:section];
}
这是我逐节创建tableview单元格的地方。masterplacesarray是保存uitableview数据的数组
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"PlacesCell";
UITableViewCell *cell = [self.mainTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
FMDatabase *db = [FMDatabase databaseWithPath:[Utility getDatabasePath]];
[db open];
NSString *sectioname = [self.sectionsarray objectAtIndex:indexPath.section];
self.masterplacesarray = [[NSMutableArray alloc] init];
FMResultSet *resultsdata = [db executeQuery:[NSString stringWithFormat:@"SELECT ArticleTitle, ArticlePreview, id, ArticleTags FROM Articles where section='%@'",sectioname]];
while([resultsdata next])
{
Article *articledata = [[Article alloc] init];
articledata.articleid = [resultsdata stringForColumn:@"id"];
articledata.articletitle = [resultsdata stringForColumn:@"ArticleTitle"];
articledata.articlepreview = [resultsdata stringForColumn:@"ArticlePreview"];
articledata.articletags = [resultsdata stringForColumn:@"ArticleTags"];
[masterplacesarray addObject:articledata];
}
[db close];
Article *placecell = [self.masterplacesarray objectAtIndex:indexPath.row];
if (tableView == [[self searchDisplayController] searchResultsTableView])
{
placecell = [self.searchResults objectAtIndex:indexPath.row];
cell.tag = 666;
}
else
{
placecell = [self.masterplacesarray objectAtIndex:indexPath.row];
}
UILabel *childIDLabel = (UILabel *)[cell viewWithTag:999];
childIDLabel.text = placecell.articleid;
UILabel *titleLabel = (UILabel *)[cell viewWithTag:100];
titleLabel.text = placecell.articletitle;
UILabel *previewLabel = (UILabel *)[cell viewWithTag:101];
previewLabel.text = placecell.articlepreview;
// UIImageView *imageview = (UIImageView *)[cell viewWithTag:102];
// [imageview setImage:[UIImage imageNamed:peoplecell.articlethumb]];
return cell;
}
什么是变量self.placesarray?可能您应该为每行使用masterplacesarray。请提供tableview代表的完整代码。您好,谢谢您的回复。self.placesarray是self.masterplacesarray的拼写错误。我已经更正了这里的代码。这并不是造成我的问题,因为在我的实际项目中,sarray并不存在。我还用uitableview代理的其余代码更新了我的问题。我想您在-UITableViewCell*tableView:uitableview*tableView cellForRowAtIndexPath:NSIndexPath*indexPath中有问题,因为您更改了全局数据模型self.masterplacesarray。在我看来,你应该有两个数据模型,masterPlacesArray和filteredMasterPlacesArray。你是说有第二个数据模型来保存数据库表中的所有数据,然后用它来搜索吗?我指的是两个模型:所有数据和过滤数据。