【问题标题】:How to add a search Icon as section for SearchBar in UITableView using search controller如何使用搜索控制器在 UITableView 中添加搜索图标作为搜索栏的部分
【发布时间】:2013-05-20 23:09:13
【问题描述】:

我正在尝试将搜索图像(放大镜)添加为部分,以便轻松转到搜索栏。

这是我遇到的问题:

我设置这些部分的代码如下:

注意:self.fbFriends 是一个字典数组,其中包括用户的 facebook 好友。

self.fbFriends = [[NSArray alloc] initWithArray:[[MESCache sharedCache] facebookFriends]];
self.searchResults = [[NSMutableArray alloc] init];
self.sections = [[NSMutableDictionary alloc] init];

BOOL found;

[self.sections setValue:[[NSMutableArray alloc] init] forKey:UITableViewIndexSearch];

// Loop through the friends and create our keys
for (NSDictionary *friend in self.fbFriends)
{
    NSString *c = [[friend objectForKey:@"name"] substringToIndex:1];

    found = NO;

    for (NSString *str in [self.sections allKeys])
    {
        if ([str isEqualToString:c])
        {
            found = YES;
        }
    }

    if (!found)
    {
        [self.sections setValue:[[NSMutableArray alloc] init] forKey:c];
    }
}

// Loop again and sort the friends into their respective keys
for (NSDictionary *friend in self.fbFriends)
{
    [[self.sections objectForKey:[[friend objectForKey:@"name"] substringToIndex:1]] addObject:friend];
}

// Sort each section array
for (NSString *key in [self.sections allKeys])
{
    [[self.sections objectForKey:key] sortUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]]];
}

这是我的部分设置和标题视图:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return 1;
    } else {
        return [[self.sections allKeys] count];
    }
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return NSLocalizedString(@"FbFriendsSearchControllerSection", @"Fb Friends search controller - section title for search results table view");
    } else {
        return [[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section];
    }
}

有谁知道这是不正确的吗?

【问题讨论】:

    标签: ios nsarray uitableview uisearchdisplaycontroller


    【解决方案1】:

    这不是标准做法。不需要额外的部分来显示搜索。

    不要将“搜索”部分添加到您的 self.sections 字典中。相反,您只有实际数据的部分。然后在你的 tableView:sectionForSectionIndexTitle:atIndex: 方法中你可以这样做:

    - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
        NSInteger res = 0;
        if (tableView == self.tableView) {
            if (index == 0) {
                // If the user taps the search icon, scroll the table view to the top
                res = -1;
                [self.tableView setContentOffset:CGPointMake(0, 0) animated:NO];
            } else {
                res = ... // return the proper index for your data
            }
        } else {
            res = 0;
        }
    
        return res;
    }
    

    旁注 - 你真的想要一个字典数组。主数组应该代表这些部分。正如您现在所拥有的那样,您不断地获取主字典的键并对它们进行排序。这将进行很多次 - 都是不必要的。使用数组可以快速从节索引中获取数据。

    【讨论】:

    • facebook好友信息存储在一个数组中,每个好友都是数组中的一个字典。朋友的字典然后包括朋友的 ID 和姓名。然后,我遍历字典数组 (self.fbFriends),收集每个名称的第一个字母,为部分创建一个数组。您还建议如何尝试完成此操作?
    • self.sections 是每个字典名称(即朋友名称)的第一个字符的字符串字符数组,这仅在 viewDidLoad 中创建,以后不会更改,因此应该只创建一次?
    • 不,self.sectionsNSDictionary,而不是 NSArray。你怎么能打电话给allKeys呢?您希望创建一次数组,这样您就不必像在发布的代码中那样调用allKeys 并一遍又一遍地对结果数组进行排序。
    • 你能解释一下当它只在视图中运行一次时,它是如何被一遍又一遍地调用的吗?您能否提供一个代码示例,说明如何使用数组完成相同的操作?
    • 他建议将[self.sections allKeys] 拉出到确实加载的视图中。
    猜你喜欢
    • 2014-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-02
    相关资源
    最近更新 更多