iphone - Tableview 的 Plist 搜索

标签 iphone ios uitableview plist uisearchbar

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

请检查代码并让我知道如何搜索 plist.. cellforrowatindexpathnoofrowsinsection 从 plist 中搜索应该有什么变化

.

first sec .

- (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 因为您过滤的是部分标题而不是行。

关于iphone - Tableview 的 Plist 搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13987238/

相关文章:

ios - NSOperationQueue 每行 UITableView

ios - 如果在 LaunchScreen.storyboard 上使用该图像,图像在更改时不会更新

ios - 在iOS模拟器中启用两因素身份验证

ios - [NSOperationQueue mainQueue] 保证是串行的吗?

ios - 想要展开和折叠 UITableview 部分

ios - UITableViewCell 的内容

ios - 如何在iOS中生成24位True Color动画Gif?

iphone - iPhone 中的 Twitter 集成无需登录即可阅读公共(public)推文/HashTags?

iphone - 数组返回空值,arr = [[tempDict1 valueForKey : @"rates"] componentsSeparatedByString: @";"];

ios - TableView 单元格行高不起作用