联系人分组


url:http://mobile.9sssd.com/ios/art/967

#import <UIKit/UIKit.h>
2 @interface HomeViewController : UIViewController
3 <UITableViewDelegate, UITableViewDataSource>{
4 }
5 @property (nonatomic, retain) NSDictionary *contactTitles;//存储所有的联系人信息
6 @property (nonatomic, retain) NSArray *groups;//所有分类名称存入数组中
7 @end
3. HomeViewController.m中添加代码

View Row Code
1 #import "HomeViewController.h"
2 @interface HomeViewController ()
3 @end
4 @implementation HomeViewController
5 @synthesize contactTitles;
6 @synthesize groups;
7 - (void)viewDidLoad
8 {
9   
10     NSString *path = [[NSBundle mainBundle] pathForResource:@"contacts" ofType:@"plist"];//plist文件路径
11     NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
12     self.contactTitles = dict;
13     [dict release];
14    
15     NSArray *array = [[contactTitles allKeys] sortedArrayUsingSelector:@selector(compare:)];
16    
17     self.groups = array;
18     [super viewDidLoad];
19 }
20 //使用UITableViewDataSource协议的tableView:cellForRowAtIndexPath:方法
21 - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
22    
23     static NSString *CellIndentifier = @"Contact";
24    
25     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIndentifier];
26    
27     if (cell == nil) {
28         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIndentifier] autorelease];
29        
30     }
31    
32    
33        NSString *group = [groups objectAtIndex:[indexPath section]];
34     NSArray * contactSection = [contactTitles objectForKey:group];
35     cell.textLabel.text = [contactSection objectAtIndex:[indexPath row]];
36    
37    
38     //单元格添加图片
39     UIImage *image = [UIImage imageNamed:@"avatar.png"];
40     cell.imageView.image = image;
41    
42    
43     return cell;
44 }
45 - (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
46     return [groups count];
47 }
48 //使用UITableViewDataSource协议的tableView:numberOfRowsInSection:方法
49 //该方法用来设置Table View中要显示数据的行数
50 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
51
52     NSString *group = [groups objectAtIndex:section];
53     NSArray *contactSection = [contactTitles objectForKey:group];
54    
55     return [contactSection count];
56 }
57 //添加标题和脚本信息
58 - (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
59     NSString *group = [groups objectAtIndex:section];
60     return group;
61    
62 }
63 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
64     return @"作者:what if";
65 }
66
67
68 /*//UITableViewDelegate协议的方法,选择表格中的项目
69 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
70     NSString *contactSelected = [listOfContacts objectAtIndex:[indexPath row]];
71     NSString *msg = [[NSString alloc] initWithFormat:@"您选择的联系人:%@", contactSelected];
72     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"选择联系人" message:msg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
73     [alert show];
74     [alert release];
75     [contactSelected release];
76     [msg release];
77 } */
78 /*
79 //UITableViewDelegate协议的方法,表格中的缩进
80 - (NSInteger) tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{
81     return [indexPath row] % 9;
82    
83 }*/
84 //索引功能
85
86 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
87     return groups;
88 }
89 //用户点击标志后触发的事件,只有DetailDisclosure Button才有该事件
90 - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
91     //进入到该项目的详细信息页面
92 }

你可能感兴趣的:(ios)