歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> iOS 類似iPhone通訊錄TableView完整實例【源碼】

iOS 類似iPhone通訊錄TableView完整實例【源碼】

日期:2017/3/1 10:22:00   编辑:Linux編程

iOS 類似iPhone通訊錄TableView完整實例,初學objective-c,自己寫的一個demo,有不足之處請指正:

效果圖:

核心代碼:

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. NSArray *array = [[NSArray alloc] initWithObjects:@"你好", @"BFlower",
  5. @"CGrass", @"DFence", @"EHouse", @"FTable", @"GChair",
  6. @"HBook", @"ISwing" ,@"JWang" ,@"KDong" ,@"LNi" ,@"MHao" ,@"Na" ,@"Oa" ,@"Pa" ,@"Qa" ,@"Ra" ,@"Sa" ,@"Ta" ,@"Ua" ,@"Va" ,@"Wa" ,@"Xa" ,@"Ya" ,@"Za", nil];
  7. self.listarray = array;
  8. NSLog(@"listarryCount:%d",[listarray count]);
  9. 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];
  10. NSArray *arrayA = [[NSArray alloc] initWithObjects:@"測試A1",@"測試A2", nil];
  11. NSArray *arrayB = [[NSArray alloc] initWithObjects:@"測試B1",@"測試B2",@"測試B3", nil];
  12. NSArray *arrayC = [[NSArray alloc] initWithObjects:@"測試C1",@"測試C2",@"測試C3",@"測試C4", nil];
  13. NSArray *arrayD = [[NSArray alloc] initWithObjects:@"測試D1",@"測試D2",@"測試D3",@"測試D4",@"測試D5", nil];
  14. NSArray *arrayE = [[NSArray alloc] initWithObjects:@"測試E1",@"測試E2",@"測試E3",@"測試E4",@"測試E5",@"測試E6", nil];
  15. NSArray *arrayF = [[NSArray alloc] initWithObjects:@"測試F1",@"測試F2",@"測試F3",@"測試F4",@"測試F5",@"測試F6",@"測試F7", nil];
  16. NSArray *arrayG = [[NSArray alloc] initWithObjects:@"測試G1",@"測試G2",@"測試G3",@"測試G4",@"測試G5",@"測試G6", nil];
  17. arrayDictKey = [[NSArray alloc] initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G", nil];
  18. arrayDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:arrayA,[arrayDictKey objectAtIndex:0],
  19. arrayB,[arrayDictKey objectAtIndex:1],
  20. arrayC,[arrayDictKey objectAtIndex:2],
  21. arrayD,[arrayDictKey objectAtIndex:3],
  22. arrayE,[arrayDictKey objectAtIndex:4],
  23. arrayF,[arrayDictKey objectAtIndex:5],
  24. arrayG,[arrayDictKey objectAtIndex:6],
  25. nil];
  26. NSLog(@"arrayrow:%d",[[arrayDict objectForKey:[arrayDictKey objectAtIndex:1]] count]);
  27. tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)];
  28. [tableView setDelegate:self];
  29. [tableView setDataSource:self];
  30. [self.view addSubview:tableView];
  31. [tableView release];
  32. // Do any additional setup after loading the view.
  33. }
  34. - (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView
  35. {
  36. //* 出現幾組
  37. //if(aTableView == self.tableView) return 27;
  38. return [arrayDict count];
  39. }
  40. //*字母排序搜索
  41. - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
  42. {
  43. //* 字母索引列表
  44. /*NSMutableArray *toBeReturned = [[NSMutableArray alloc]init];
  45. for(char c= 'A';c<='Z';c++)
  46. [toBeReturned addObject:[NSString stringWithFormat:@"%c",c]];*/
  47. return arrayDictKey;
  48. /*NSMutableArray *newarr=[[NSMutableArray alloc]initWithArray:listarray];
  49. [newarr addObject:@"{search}"]; //等價於[arr addObject:UITableViewIndexSearch];
  50. return newarr;*/
  51. }
  52. - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
  53. {
  54. //搜索時顯示按索引第幾組
  55. NSInteger count = 0;
  56. NSLog(@"%@",title);
  57. for(NSString *character in arrayDictKey)
  58. {
  59. if([character isEqualToString:title])
  60. {
  61. return count;
  62. }
  63. count ++;
  64. }
  65. return count;
  66. }
  67. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  68. {
  69. /*if([listarray count]==0)
  70. {
  71. return @"";
  72. }*/
  73. //return [listarray objectAtIndex:section]; //*分組標簽
  74. return [arrayDictKey objectAtIndex:section];
  75. }
  76. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  77. //return [self.listarray count]; //*每組要顯示的行數
  78. //NSInteger i = [[listarray objectAtIndex:section] ]
  79. NSInteger i = [[arrayDict objectForKey:[arrayDictKey objectAtIndex:section]] count];
  80. return i;
  81. }
  82. -(UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath{
  83. //返回類型選擇按鈕
  84. return UITableViewCellAccessoryDisclosureIndicator; //每行右邊的圖標
  85. }
  86. - (UITableViewCell *)tableView:(UITableView *)tableview
  87. cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  88. static NSString *TableSampleIdentifier = @"TableSampleIdentifier";
  89. UITableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:
  90. TableSampleIdentifier];
  91. if (cell == nil) {
  92. cell = [[UITableViewCell alloc]
  93. initWithStyle:UITableViewCellStyleDefault
  94. reuseIdentifier:TableSampleIdentifier];
  95. }
  96. NSUInteger row = [indexPath row];
  97. NSUInteger sectionMy = [indexPath section];
  98. NSLog(@"sectionMy:%d",sectionMy);
  99. cell.textLabel.text = [[arrayDict objectForKey:[arrayDictKey objectAtIndex:sectionMy]] objectAtIndex:row]; //每一行顯示的文字
  100. NSString *str= [NSString stringWithFormat: @"%d", row];
  101. UIImage *image = [UIImage imageNamed:str];
  102. cell.imageView.image = image;
  103. UIImage *highLighedImage = [UIImage imageNamed:@"1.png"];
  104. cell.imageView.highlightedImage = highLighedImage; //選中一行時頭部圖片的改變
  105. return cell;
  106. }
  107. //劃動cell是否出現del按鈕
  108. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  109. return YES; //是否需要刪除圖標
  110. }
  111. //編輯狀態(不知道干什麼用)
  112. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
  113. forRowAtIndexPath:(NSIndexPath *)indexPath
  114. {
  115. [self viewDidLoad];
  116. }
  117. //選中時執行的操作
  118. - (NSIndexPath *)tableView:(UITableView *)tableView
  119. willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  120. NSUInteger row = [indexPath row];
  121. if (row%2 == 0) {
  122. NSUInteger row = [indexPath row];
  123. NSString *rowValue = [listarray objectAtIndex:row];
  124. NSString *message = [[NSString alloc] initWithFormat:
  125. @"You selected %@", rowValue];
  126. UIAlertView *alert = [[UIAlertView alloc]
  127. initWithTitle:@"Row Selected!"
  128. message:message
  129. delegate:nil
  130. cancelButtonTitle:@"Yes I Did"
  131. otherButtonTitles:nil];
  132. [alert show];
  133. }
  134. return indexPath;
  135. }
  136. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  137. //這裡控制值的大小
  138. return 50.0; //控制行高
  139. }

總結:有些東西沒用之前覺得這個會很難,很復雜,做過後才發現,其實關鍵在於理解,多看多實踐這樣才能提高。

Demo下載地址:

下載在Linux公社的1號FTP服務器裡,下載地址:

FTP地址:ftp://www.linuxidc.com

用戶名:www.linuxidc.com

密碼:www.muu.cc

在 2012年LinuxIDC.com\5月\iOS 類似iPhone通訊錄TableView完整實例【源碼】

下載方法見 http://www.linuxidc.net/thread-1187-1-1.html

Copyright © Linux教程網 All Rights Reserved