搜索显示控制器结果仅来自我的UITableView的第一部分

时间:2012-03-18 11:57:54

标签: ios uitableview uisearchbar

我正在使用带有搜索显示控制器的UITableView。我的问题是,在搜索过程中,只有表格第一部分中的行显示在结果中。

我的代码如下:

首先在ViewDidLoad中我得到了各个部分,我创建了搜索结果数组,其容量等于我的数据库表中的记录总数。

- (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];

}

以下代码负责进行实际搜索。它在数组中查找名为ArticleTags的记录的任何部分中的搜索项,该数组包含UITableView的数据。

- (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;
}

0 个答案:

没有答案