Tableview的Plist搜索 [英] Plist Search of Tableview

查看:82
本文介绍了Tableview的Plist搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 Plist ,它已在表格视图中填充了扩展的部分..现在我想要搜索表格..在图片中你可以看到发生了什么我搜索任何东西。

只是因为我正在搜索它,但需要对搜索结果的cellforrowatindexpath进行一些更改....

I have Plist which is been populated on the tableview with expanded sections ..now i want to search the table..below in images you can see what is happening when I search anything. . just because I am searching it but need some changes in the cellforrowatindexpath for search results....

请检查代码并让我知道该怎么做用于搜索plist ..
对于来自plist的搜索 cellforrowatindexpath noofrowsinsection 应该有什么变化

please check the code and let me know what to do for searching plist.. what should be changes for the cellforrowatindexpath and noofrowsinsection for search from plist



.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{


return [self.mySections count];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
 NSInteger rows = 0;


if ([self tableView:tableView canCollapseSection:section] || !(tableView == self.searchDisplayController.searchResultsTableView) )
{
    if ([expandedSections containsIndex:section] )
    {

        NSString *key = [self.mySections objectAtIndex:section];
        NSArray *dataInSection = [[self.myData objectForKey:key] objectAtIndex:0];

        return [dataInSection count];


    }
    return 1;
 } else if(tableView == self.searchDisplayController.searchResultsTableView) {

     rows = [self.searchResults count];
    return rows;
}

    return 1;


}

 -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger)section {
NSString *key = [self.mySections objectAtIndex:section];
return [NSString stringWithFormat:@"%@", key];
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {

  //some changes required  to display plst
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] ;
}

// Configure the cell...



if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]) {
    cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row];
}else {

NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];



NSString *key = [self.mySections objectAtIndex:section];

NSDictionary *dataForSection = [[self.myData objectForKey:key] objectAtIndex:0];
NSArray *array=dataForSection.allKeys;

cell.textLabel.text = [[dataForSection allKeys] objectAtIndex:row];    
cell.detailTextLabel.text=[dataForSection valueForKey:[array objectAtIndex:indexPath.row]];
}

return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self tableView:tableView canCollapseSection:indexPath.section])
{
    if (!indexPath.row)
    {
        // only first row toggles exapand/collapse
        [tableView deselectRowAtIndexPath:indexPath animated:YES];

        NSInteger section = indexPath.section;
        BOOL currentlyExpanded = [expandedSections containsIndex:section];
        NSInteger rows;


        NSMutableArray *tmpArray = [NSMutableArray array];

        if (currentlyExpanded)
        {
            rows = [self tableView:tableView numberOfRowsInSection:section];
            [expandedSections removeIndex:section];

        }
        else
        {
            [expandedSections addIndex:section];
            rows = [self tableView:tableView numberOfRowsInSection:section];
        }


        for (int i=1; i<rows; i++)
        {
            NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i 
                                                           inSection:section];
            [tmpArray addObject:tmpIndexPath];
        }

        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

        if (currentlyExpanded)
        {
            [tableView deleteRowsAtIndexPaths:tmpArray 
                             withRowAnimation:UITableViewRowAnimationTop];

            cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown];

        }
        else
        {
            [tableView insertRowsAtIndexPaths:tmpArray 
                             withRowAnimation:UITableViewRowAnimationTop];
            cell.accessoryView =  [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp];

        }
    }
}
}

- (void)filterContentForSearchText:(NSString*)searchText 
                         scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate 
                                predicateWithFormat:@"SELF contains[cd] %@",
                                searchText];

self.searchResults = [self.mySections filteredArrayUsingPredicate:resultPredicate];

}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
UISearchBar * searchBar = [controller searchBar];
[self filterContentForSearchText:searchString scope:[[searchBar scopeButtonTitles] objectAtIndex:[searchBar selectedScopeButtonIndex]]];
return YES;
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
UISearchBar * searchBar = [controller searchBar];
[self filterContentForSearchText:[searchBar text] scope:[[searchBar scopeButtonTitles] objectAtIndex:searchOption]];
return YES;
}


推荐答案

您正在使用的搜索结果numberOfRows应该用于numberOfSections
因为你要过滤节标题而不是行。

The search results you are using in numberOfRows should be used for numberOfSections Since you are filtering for section title and not rows.

这篇关于Tableview的Plist搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆