使用搜索更新数组

时间:2014-01-21 11:41:48

标签: objective-c nsarray

我正在尝试修改表格视图中的NSMutableArray directoryList以搜索结果。 搜索时表视图正确更新。 但是,当通过选择行访问directoryList中的文件时,结果基于未搜索的directoryList

这就是我填充数组的方式:

- (void) myDirectoryLogFunction
{
    NSLog(@"This is the log");

    NSString *path1 = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Logs/"];
    NSString *path2 = @"/Library/Logs/";

    directoryList1 = [[[NSFileManager defaultManager] subpathsOfDirectoryAtPath:path1 error:nil]
                           pathsMatchingExtensions:[NSArray arrayWithObjects:@"log", nil]];

    directoryList2 = [[[NSFileManager defaultManager] subpathsOfDirectoryAtPath:path2 error:nil]
                           pathsMatchingExtensions:[NSArray arrayWithObjects:@"log", nil]];

    directoryList = [NSMutableArray array];
    [directoryList addObjectsFromArray:directoryList1];
    [directoryList addObjectsFromArray:directoryList2];

    NSMutableArray *rows = [NSMutableArray array];

    for (NSUInteger i = 0; i < directoryList.count; i++)
    {                      
        [rows addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[directoryList objectAtIndex:i], @"Logs", nil]];
    }

    [_LogListController setContent:rows];
    [logsTableView reloadData];
}

这是我的行选择方法:

- (void) tableViewSelectionDidChange: (NSNotification *) notification
{
    NSInteger selectedRow = [logsTableView selectedRow];

    if (selectedRow <= directoryList1.count)
    {
        NSLog(@"row is: %lu", selectedRow);

        NSString *filePath = [NSHomeDirectory() stringByAppendingFormat:@"/Library/Logs/%@", [directoryList objectAtIndex:selectedRow]];

        NSString *content = [NSString stringWithContentsOfFile:filePath
                                              encoding:NSUTF8StringEncoding
                                                 error:NULL];

        if (content != NULL)
        {
            [logsScrollViewTextView setString:content];
        } else
        {
            [logsScrollViewTextView setString:@"No permission to read log"];
        }
    } else
    {
        NSLog(@"row is: %lu", selectedRow);

        NSString *filePath = [NSString stringWithFormat:@"/Library/Logs/%@", [directoryList objectAtIndex:selectedRow]];

        NSString *content = [NSString stringWithContentsOfFile:filePath
                                                  encoding:NSUTF8StringEncoding
                                                     error:NULL];

        if (content != NULL)
        {
            [logsScrollViewTextView setString:content];
        } else
        {
            [logsScrollViewTextView setString:@"No permission to read log"];
        }
    }
}

这是我的搜索方法:

-(IBAction)controlTextDidEndEditing:(id)sender
{
    if ([[[self mySearchField] stringValue] isEqualToString:@""])
    {
        NSPredicate *filter = [NSPredicate predicateWithFormat:@"(Logs contains[cd] Logs)"];
        [_LogListController setFilterPredicate: filter];
        // directoryList.count when testing remains on un-searched directoryList
        NSLog(@"directoryList.count is: %lu", directoryList.count);

    } else
    {
        NSString *_searchString = [[self mySearchField] stringValue];

        NSPredicate *_searchResults = [NSPredicate predicateWithFormat:@"(Logs contains[cd] %@)", _searchString];
        [_LogListController setFilterPredicate: _searchResults];
        NSLog(@"directoryList.count is: %lu", directoryList.count);
    }
}

2 个答案:

答案 0 :(得分:0)

如果您正在搜索(过滤)源数据,那么您的选择代码需要使用该数据源。因此,使用if (selectedRow <= directoryList1.count)将无效,因为原始未过滤数据的计数错误。

相反,检查存在,而不是计数:

if ([directoryList1 containsObject:selectedObject])

其中selectedObject是从所选行的已过滤列表中获取的对象(看起来像[directoryList objectAtIndex:selectedRow])。

您需要明确过滤和未过滤的数据及其使用方式。另请注意,如果2个阵列具有相同名称的文件,则可能会遇到问题......


您可能想要考虑只有一个数组,而不是2.将完整的文件路径放入数组中,然后您不必担心重复。要显示,请使用路径的lastPathComponent

有另一个数组:

if (!isSearching) {
    dataSourceArray = directoryListArray;
} else {
    dataSourceArray = [directoryListArray filteredArrayUsingPredicate:...];
}

这使代码更清晰,更简单。

答案 1 :(得分:0)

我想你需要这样的东西:

- (void) tableViewSelectionDidChange: (NSNotification *) notification
{
    NSInteger selectedRow = [logsTableView selectedRow];

    if (selectedRow <= directoryList1.count)
    {
        NSLog(@"row is: %lu", selectedRow);
        NSArray *filteredArray = [directoryList filteredArrayUsingPredicate: filter];

        NSString *filePath = [NSHomeDirectory() stringByAppendingFormat:@"/Library/Logs/%@", [filteredArray objectAtIndex:selectedRow]];

        NSString *content = [NSString stringWithContentsOfFile:filePath
                                              encoding:NSUTF8StringEncoding
                                                 error:NULL];

        if (content != NULL)
        {
            [logsScrollViewTextView setString:content];
        } else
        {
            [logsScrollViewTextView setString:@"No permission to read log"];
        }
    } else
    {
        NSLog(@"row is: %lu", selectedRow);

        NSString *filePath = [NSString stringWithFormat:@"/Library/Logs/%@", [directoryList objectAtIndex:selectedRow]];

        NSString *content = [NSString stringWithContentsOfFile:filePath
                                                  encoding:NSUTF8StringEncoding
                                                     error:NULL];

        if (content != NULL)
        {
            [logsScrollViewTextView setString:content];
        } else
        {
            [logsScrollViewTextView setString:@"No permission to read log"];
        }
    }
}