Tableview的Plist搜索

Posted

技术标签:

【中文标题】Tableview的Plist搜索【英文标题】:Plist Search of Tableview 【发布时间】:2012-12-21 09:14:24 【问题描述】:

我有Plist,它在表格视图中填充了扩展部分..现在我想搜索表格..在下面的图像中你可以看到当我搜索任何东西时发生了什么。 . 只是因为我正在搜索它,但需要在 cellforrowatindexpath 中对搜索结果进行一些更改....

请检查代码,让我知道如何搜索 plist.. 从 plist 搜索的 cellforrowatindexpathnoofrowsinsection 应该有哪些变化

.

.

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

【问题讨论】:

【参考方案1】:

您在 numberOfRows 中使用的搜索结果应该用于 numberOfSections 由于您正在过滤部分标题而不是行。

【讨论】:

以上是关于Tableview的Plist搜索的主要内容,如果未能解决你的问题,请参考以下文章

在 tableview 的文本字段中搜索

为啥在 searchBar 和 tableview 之间添加空格?

不搜索第一个字母

iOS7 - 搜索结果 tableview 下推 20px

如何使用 json 而不是 plist 来填充 TableView

SWIFT 属性列表:从 TableView 到 DetailView plist