iphone - 向使用字母部分的 iPhone 应用程序添加搜索栏?

标签 iphone objective-c ios uitableview uisearchbar

我有一个使用包含产品名称的 UITableView 的应用程序,这些产品也根据其首字母分为各自的部分。

这是我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

        //Initialize alphabet array
        m_Alphabet = [[NSArray alloc]initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K", @"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",@"Other", nil];

        //Initialize alphabet distionary of arrays
        m_AlphabetDictionary = [[NSMutableArray alloc]init];

        //Populate distionary with a mutable array for each character
        //in the alphabet (plus one "Other" category)
        for (int i = 0; i < 27; i++)
            [m_AlphabetDictionary insertObject:[[NSMutableArray alloc] init] atIndex: i];

        // The number of products in the database
        appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

        NSMutableArray *tempArray = [[NSMutableArray alloc] init];

        //For each product in the appDelgates products
        for (Product *product in appDelegate.m_Products){
            if ([product.category isEqualToString:productType]){

                [tempArray addObject:product];

                //firstLetter is equal to the first letter of the products name
                NSString * l_FirstLetter = [product.name substringToIndex:1];
                //convert firstString to uppercase
                l_FirstLetter = [l_FirstLetter uppercaseString];

                //The added flag ensures objects whose first letter isn't a letter
                //are added to array 26
                bool added = NO;

                for(int i=0; i<[m_Alphabet count]; i++){
                    //If the first letter of product name is equal to the letter at index i in theAlphabet
                    if ([l_FirstLetter isEqualToString:[m_Alphabet objectAtIndex:i]]) {
                        //Add product to section array for that letter
                        [[m_AlphabetDictionary objectAtIndex:i] addObject:product];

                        added = YES;
                    }

                }
                //If product hasn't been added to array, add it to "Others" category
                if(!added)
                    [[m_AlphabetDictionary objectAtIndex:26] addObject:product];

            }
        }

    //Set navigation controller title
    self.title = productType;

}

//Number of sections is equal to the length of the m_Alphabet array
//Letters A-Z plus "Other"
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
        return [m_Alphabet count]; 
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
        return [[m_AlphabetDictionary objectAtIndex:section] count]; 
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 
{
    return m_Alphabet; 
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index 
{
    return [m_Alphabet indexOfObject:title]; 
}

- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section
{
    if ([[m_AlphabetDictionary objectAtIndex:section] count]==0) 
        return nil;

    return [m_Alphabet objectAtIndex:section]; 
}

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

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

    if([self.productType isEqualToString:@"All"]){
        Product *product = (Product *) [appDelegate.m_Products objectAtIndex:indexPath.row];

        cell.textLabel.text = product.name;

        // Configure the cell.
        return cell;
    }
    else {

            //Instead of using appDelegate.products use the new array that will be filled
            //by the numberOfReowsInSection method
            Product *product = (Product *)[[m_AlphabetDictionary objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

            cell.textLabel.text = product.name;
            // Configure the cell.
            return cell;
    }
}

我想要做的是在我的表格 View 顶部添加一个搜索栏,它的行为就像 iPhone 联系人应用程序中“所有联系人”部分中的搜索栏一样。 IE。当我搜索时,所有部分都消失了,只显示搜索结果,直到搜索栏再次变为空白。

谁能指出我正确的方向?

谢谢,

jack

最佳答案

看起来您需要使用 UISearchDisplayController ,这很可能是联系人应用程序使用的(以及邮件、 map 、Safari 和音乐)。它以 TableView 覆盖的形式呈现,可能包含也可能不包含范围栏(您的选择),并根据搜索栏的文本过滤结果。可以找到涉及界面生成器的简单教程 here , 和一个苹果示例 sans-IB 可以找到 here .

关于iphone - 向使用字母部分的 iPhone 应用程序添加搜索栏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10064869/

相关文章:

ios - 如何使用 IBAFormSection 减小宽度?

ios - 当 ios 中该部分数据为空时,我如何删除 uitableview 部分?

ios - DispatchQueue 在 Swift 中不起作用的问题

ios - 我怎样才能让应用程序出现在 iTunes 的应用程序选项卡中?

objective-c - NSSlider 多次撤销注册

objective-c - 将swift文件导入objective-c项目

ios - 如何同时支持 iOS 7 和 8 的 NSCalendar?

iphone - 使用 [UIColor colorWithRed :green:blue:alpha:] doesn't work with UITableView seperatorColor?

iphone - 推送新 View 时删除工具栏

iphone - 有没有办法识别用户iPhone的访问并自动调整网页以适应iPhone屏幕尺寸?