简体   繁体   English

实现搜索栏iOS 6表格视图静态单元格

[英]Implementing a search bar iOS 6 table view static cell

Im trying to implement a search bar on a Static Cell table view. 我试图在静态单元格表​​视图上实现搜索栏。

I have subclassed the UITableview controller, declared the Search Bar and Search Display Controller in .h and also set <UISearchDisplayDelegate> 我将UITableview控制器子类化,在.h中声明了Search Bar and Search Display Controller ,还设置了<UISearchDisplayDelegate>

Now im unsure what to do next in my .m file, the tutorials ive looked at all refer to prototype cells and I cant seem to adapt the code for my requirements. 现在我不确定在.m文件中下一步要做什么,本教程ive都看了所有有关原型单元的内容,我似乎无法适应我的要求的代码。 I have also looked at Apples sample code 我也看过苹果的示例代码

My end result is to search the cells on the page from the search bar. 我的最终结果是从搜索栏搜索页面上的单元格。 My search bar brings up the rows but does not sort them according the the search. 我的搜索栏显示行,但不根据搜索对行进行排序。 Further to that, when returning to the table view it seems corrupted graphically. 除此之外,当返回到表格视图时,它似乎在图形上已损坏。

Using Xcode 4.5 使用Xcode 4.5

There is a lot being asked in that question. 在这个问题上有很多问题要问。 Here are some tips: 这里有一些提示:

Add the search bar to your table header 将搜索栏添加到表格标题

self.tableView.tableHeaderView = self.searchBar;

Hide this by default, when you scroll down it will appear 默认情况下隐藏它,向下滚动时会出现

[self.tableView setContentOffset:CGPointMake(0, 44)];

As for searching / filtering the table. 至于搜索/过滤表。 What you need is the table datasource methods pointing at an array of data. 您需要的是指向数据数组的表数据源方法。 You also need to keep another array of the full list of data. 您还需要保留完整数据列表的另一个数组。 On searching, call a method like this: 搜索时,调用如下方法:

    - (void) performSearchWithText:(NSString *)searchText {
       if ([searchText length] > 0) {
            NSPredicate *predicate = [NSPredicate predicateWithFormat:@"cell contains[cd]   %@", searchText]; 
            NSArray *filteredArray = [self.allData filteredArrayUsingPredicate:predicate];
            self.tableData = [NSArray arrayWithArray:filteredArray];
            [self.tableView reloadData];
        } else {
            self.tableData = self.allData;
            [self.tableView reloadData];   
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM