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

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章