iOS 类似iPhone通讯录TableView完整实例,初学objective-c,自己写的一个demo,有不足之处请指正:
效果图:
核心代码:
1.- (void)viewDidLoad
2.{
3. [super viewDidLoad];
4.
5. NSArray *array = [[NSArray alloc] initWithObjects:@"你好", @"BFlower",
6. @"CGrass", @"DFence", @"EHouse", @"FTable", @"GChair",
7. @"HBook", @"ISwing" ,@"JWang" ,@"KDong" ,@"LNi" ,@"MHao" ,@"Na" ,@"Oa" ,@"Pa" ,@"Qa" ,@"Ra" ,@"Sa" ,@"Ta" ,@"Ua" ,@"Va" ,@"Wa" ,@"Xa" ,@"Ya" ,@"Za", nil];
8. self.listarray = array;
9. NSLog(@"listarryCount:%d",[listarray count]);
10. secLabelArray = [[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", nil];
11.
12. NSArray *arrayA = [[NSArray alloc] initWithObjects:@"测试A1",@"测试A2", nil];
13. NSArray *arrayB = [[NSArray alloc] initWithObjects:@"测试B1",@"测试B2",@"测试B3", nil];
14. NSArray *arrayC = [[NSArray alloc] initWithObjects:@"测试C1",@"测试C2",@"测试C3",@"测试C4", nil];
15. NSArray *arrayD = [[NSArray alloc] initWithObjects:@"测试D1",@"测试D2",@"测试D3",@"测试D4",@"测试D5", nil];
16. NSArray *arrayE = [[NSArray alloc] initWithObjects:@"测试E1",@"测试E2",@"测试E3",@"测试E4",@"测试E5",@"测试E6", nil];
17. NSArray *arrayF = [[NSArray alloc] initWithObjects:@"测试F1",@"测试F2",@"测试F3",@"测试F4",@"测试F5",@"测试F6",@"测试F7", nil];
18. NSArray *arrayG = [[NSArray alloc] initWithObjects:@"测试G1",@"测试G2",@"测试G3",@"测试G4",@"测试G5",@"测试G6", nil];
19. arrayDictKey = [[NSArray alloc] initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G", nil];
20. arrayDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:arrayA,[arrayDictKey objectAtIndex:0],
21. arrayB,[arrayDictKey objectAtIndex:1],
22. arrayC,[arrayDictKey objectAtIndex:2],
23. arrayD,[arrayDictKey objectAtIndex:3],
24. arrayE,[arrayDictKey objectAtIndex:4],
25. arrayF,[arrayDictKey objectAtIndex:5],
26. arrayG,[arrayDictKey objectAtIndex:6],
27. nil];
28. NSLog(@"arrayrow:%d",[[arrayDict objectForKey:[arrayDictKey objectAtIndex:1]] count]);
29.
30. tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)];
31. [tableView setDelegate:self];
32. [tableView setDataSource:self];
33. [self.view addSubview:tableView];
34. [tableView release];
35.
36.
37. // Do any additional setup after loading the view.
38.}
39.
40.- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView
41.{
42. //* 出现几组
43. //if(aTableView == self.tableView) return 27;
44. return [arrayDict count];
45.}
46.
47.//*字母排序搜索
48.- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
49.{
50. //* 字母索引列表
51. /*NSMutableArray *toBeReturned = [[NSMutableArray alloc]init];
52.
53. for(char c= 'A';c<='Z';c++)
54.
55. [toBeReturned addObject:[NSString stringWithFormat:@"%c",c]];*/
56.
57. return arrayDictKey;
58.
59. /*NSMutableArray *newarr=[[NSMutableArray alloc]initWithArray:listarray];
60. [newarr addObject:@"{search}"]; //等价于[arr addObject:UITableViewIndexSearch];
61. return newarr;*/
62.
63.}
64.
65.- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
66.{
67. //搜索时显示按索引第几组
68. NSInteger count = 0;
69. NSLog(@"%@",title);
70. for(NSString *character in arrayDictKey)
71. {
72.
73. if([character isEqualToString:title])
74. {
75.
76. return count;
77.
78. }
79.
80. count ++;
81.
82. }
83.
84. return count;
85.
86.}
87.
88.- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
89.{
90.
91. /*if([listarray count]==0)
92.
93. {
94.
95. return @"";
96.
97. }*/
98.
99. //return [listarray objectAtIndex:section]; //*分组标签
100. return [arrayDictKey objectAtIndex:section];
101.
102.}
103.
104.- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
105. //return [self.listarray count]; //*每组要显示的行数
106. //NSInteger i = [[listarray objectAtIndex:section] ]
107. NSInteger i = [[arrayDict objectForKey:[arrayDictKey objectAtIndex:section]] count];
108. return i;
109.}
110.
111.-(UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath{
112. //返回类型选择按钮
113.
114. return UITableViewCellAccessoryDisclosureIndicator; //每行右边的图标
115.}
116.- (UITableViewCell *)tableView:(UITableView *)tableview
117. cellForRowAtIndexPath:(NSIndexPath *)indexPath {
118.
119. static NSString *TableSampleIdentifier = @"TableSampleIdentifier";
120.
121. UITableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:
122. TableSampleIdentifier];
123. if (cell == nil) {
124. cell = [[UITableViewCell alloc]
125. initWithStyle:UITableViewCellStyleDefault
126. reuseIdentifier:TableSampleIdentifier];
127. }
128.
129. NSUInteger row = [indexPath row];
130. NSUInteger sectionMy = [indexPath section];
131. NSLog(@"sectionMy:%d",sectionMy);
132.
133. cell.textLabel.text = [[arrayDict objectForKey:[arrayDictKey objectAtIndex:sectionMy]] objectAtIndex:row]; //每一行显示的文字
134.
135. NSString *str= [NSString stringWithFormat: @"%d", row];
136.
137. UIImage *image = [UIImage imageNamed:str];
138. cell.imageView.image = image;
139. UIImage *highLighedImage = [UIImage imageNamed:@"1.png"];
140.
141. cell.imageView.highlightedImage = highLighedImage; //选中一行时头部图片的改变
142. return cell;
143.}
144.//划动cell是否出现del按钮
145.- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
146.
147.
148. return YES; //是否需要删除图标
149.}
150.//编辑状态(不知道干什么用)
151.- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
152.forRowAtIndexPath:(NSIndexPath *)indexPath
153.{
154.
155. [self viewDidLoad];
156.}
157.
158.//选中时执行的操作
159.- (NSIndexPath *)tableView:(UITableView *)tableView
160. willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
161. NSUInteger row = [indexPath row];
162. if (row%2 == 0) {
163. NSUInteger row = [indexPath row];
164. NSString *rowValue = [listarray objectAtIndex:row];
165.
166. NSString *message = [[NSString alloc] initWithFormat:
167. @"You selected %@", rowValue];
168. UIAlertView *alert = [[UIAlertView alloc]
169. initWithTitle:@"Row Selected!"
170. message:message
171. delegate:nil
172. cancelButtonTitle:@"Yes I Did"
173. otherButtonTitles:nil];
174. [alert show];
175.
176. }
177. return indexPath;
178.}
179.- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
180.
181. //这里控制值的大小
182. return 50.0; //控制行高
183.
184.}
总结:有些东西没用之前觉得这个会很难,很复杂,做过后才发现,其实关键在于理解,多看多实践这样才能提高。
本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http://www.linuxidc.com/Linux/2012-05/61206.htm