iOS 分组索引和索引分区

发布时间:2020-06-05 22:24:48 作者:HDDevTeam
来源:网络 阅读:642


1.新建工程名SearchViewController,File->New->Projectr->SingleView Application->next

2.添加协议和声明变量

还需要让类遵循UISearchBarDelegate协议,除了充当表视图的委托之外还需要让它充当搜索栏的委托。

@interface ViewController :UIViewController<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>

{

UITableView*tableavaieGroup;

UISearchBar *search;

NSMutableDictionary *names;

NSMutableArray *keys;

BOOL isSearching;

}

3.添加plist文件,再到ViewDidLoad中初始化视图

iOS 分组索引和索引分区


- (void)viewDidLoad

{

//读取plist文件

NSString *path=[[NSBundlemainBundle]pathForResource:@"Property List"ofType:@"plist"];

NSDictionary *dictionary=[[NSDictionaryalloc]initWithContentsOfFile:path];

//self.names=dictionary;

self.allNames=dictionary;

//读取字典中的键存储在数组中

//NSArray *array=[[namesallKeys]sortedArrayUsingSelector:@selector(compare:)];

//self.myKey=array;

   [selfresetSearch];

   [tableViewGroupreloadData];

   [tableViewGroupsetContentOffset:CGPointMake(0.0, 44.0) animated:NO];

[superviewDidLoad];}

4.实现委托方法

从数组中读取字典中的plist文件有几个分区

方法获取一个可重用单元,如果单元不存在则创建一个新的单元。然后从对应查询的数组中获取对象,将单元的文本设置成控制器标题并返回单元

- (UITableViewCell *)tableView:(UITableView *)tableView

cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

staticNSString*DisclosureCellIdentifier = @"DisclosureCellIdentifier";

UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:DisclosureCellIdentifier];

if (cell == nil)

   {//创建一个新的单元

   cell=[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:DisclosureCellIdentifier];

}

//对应查询数组中获取的对象,将单元文本设置成控制器标题并返回单元

NSUInteger row=[indexPath row];

NSString *rowData=[listDataobjectAtIndex:row];

   cell.textLabel.text=rowData;

cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;

return cell;

}

5.添加搜索栏委托方法

搜索栏有许多在其委托上调用的方法,当用户单击键盘上的返回按钮或搜索键时,将调用searchBarBookmarkButtonClicked此方法从搜索栏获取搜索短语,并调用我们的搜索方法,这个搜索方法将删除names中不匹配的名称和keys中的空分区:

-(void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar

{

NSString*searchTerm=[searchBar text];

   [selfhandleSearchForTerm:searchTerm];

}


运行结果如下:


iOS 分组索引和索引分区iOS 分组索引和索引分区

(点搜索框行搜索“egg”,出第二个面)

6)NSDictinary遵循NSMutableCopying协议该协议返回一个NSmutableDictionary,但是个方法建的是浅副本。也就是说调用mutableCopy方法时,它将创建一个新的NSmutableDictionary象,该对有源字典所有的所有象,他并不是副本,而是相同的实际对象,由于字典中存有数组,如果我们从副本的数组中删除对象,这些对象也会从原字典的数组中删除,因为副本和源都指向相同的对象,在这种情况下元数组是不可改变

的所以无法删除对象,解决这个问题,需要为存有数组的字典创建一个深层可变副本,

iOS 分组索引和索引分区


Next->  之后 Categroy on (NSDictionary) Categrpy(MutableDeepCopy)

此方法创建一个新的可变字典,然后在原字典中所有的键进行迭代,为它遇到的每个数组创建可变副本。

-(NSMutableDictionary *)mutableDeepCopy

{

NSMutableDictionary *returnDict=[[NSMutableDictionaryalloc] initWithCapacity:[selfcount]];

NSArray *keys=[selfallKeys];

//NSArray *keys=[self ];

//    因为在原有的mutableCopy , 只会对第一层对象进行mutable.

//    比如NSDictionary嵌套NSArray的话.

//    原有的MutableCopy只会返回NSMutableDictionary, 而不会使其子元素也变为NSMutableArray.

//    而这种方法实际上类似递归的结构.

//循环读取复制每一个元素

for(id key in keys)

   {

id oneValue=[selfvalueForKey:key];

id oneCopy=nil;

//如果key对应的元素可以响应mutableDeepCopy方法(还是NSDictionary),调用mutableDeepCopy方法复制

if([oneValue respondsToSelector:@selector(mutableDeepCopy)])

           oneCopy=[oneValue mutableDeepCopy];

elseif([oneValue respondsToSelector:@selector(mutableCopy)])

           oneCopy=[oneValue mutableCopy];

if(oneCopy==nil)

           oneCopy=[oneValue copy];

       [returnDict setValue:oneCopy forKey:key];

   }

return returnDict;

}


7)要想在运行时搜索框后有一个Cancel按钮只需在Option中勾选Shows Cancel Button即可,如下图:

iOS 分组索引和索引分区

当用户单击Cancel按钮时,程序会将搜索短语设置为空,然后重置搜索,并重新加载数据一显示所有名称,此外,还要让搜索栏放弃第一响应者状态,这样键盘就不再起作用,以便于用户重新处理表视图。


推荐阅读:
  1. (ios实现)用c/c++混合编程方式为ios/android实现一个自绘日期选择控件(二)
  2. iOS UITableView表格索引

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

新建 search interface

上一篇:Hadoop文件合并——Hadoop In Action上的一个示例

下一篇:ShowDoc的搭建

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》