iOS學習之分段Table View的使用(Grouped樣式表格)

   簡介:上篇作了Table View的一些介紹 ,還作了一個TableView 的Plain樣式的例子,這篇咱們學習Grouped樣式表的例子,還有用到前面讀取Plist的知識(見iOS學習之 plist文件的讀寫),把Plist文件中的數據讀取出來,放到Table view裏展現出來。這裏把全國30多個省份的城市,都列出來了,plist文件裏還有城市的行政區,不過這裏只列出省份和城市就ok了。效果圖以下:git




那麼開始吧。github

一、新建項目數組

新的一個名稱爲TableViewGrouped的Single View Application項目,打開項目的xib文件,拖拽TableView控件到xib文件中,擺正位置。學習


二、給新建的TableView找到他的歸屬atom

選中新添的TableView ,Connection Inspector,找到delegatedatasource,從它們右邊的圓圈拉線到Files Owner圖標上,參考上篇的第3步:spa


三、設置Table View的屬性爲Grouped樣式.net




四、導入plist文件orm

從其餘文件夾導入Provineces.plist文件,這個文件我會傳到源代碼裏,你們都能方便使用了,包括全國30個省份和城市,還有城市的區也有。blog

 

五、添加.h .m的實現代碼。ci

.h文件添加一個property

[cpp] view plaincopy

  1. #import <UIKit/UIKit.h>  

  2.   

  3. @interface ViewController : UIViewController  

  4. @property (strong, nonatomic) NSArray *provinces;  

  5. @end  



第一步從Plist讀取出數據,第二步給Table添加數據。

在viewDidLoad讀取Plist,plist是個array類型的,因此使用Array讀取。

.m文件的實現。

[cpp] view plaincopy

  1. @implementation ViewController  

  2. @synthesize provinces;  

  3.   

  4. - (void)viewDidLoad  

  5. {  

  6.     [super viewDidLoad];  

  7.     // Do any additional setup after loading the view, typically from a nib.  

  8.     NSBundle *bundle = [NSBundle mainBundle];  

  9.     NSString *plistPath = [bundle pathForResource:@"Provineces" ofType:@"plist"];  

  10.     NSMutableArray *array=[[NSMutableArray  alloc] initWithContentsOfFile:plistPath];  

  11.     self.provinces = array;  

  12.   

  13. }  


實現TableView表格部分,下面這些方法看方法名就能大概明白意思。

  •  這個方法用來告訴表格有幾個分組

[cpp] view plaincopy

  1. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {  

  2.     return [provinces count];  

  3. }  

  • 這個方法告訴表格第section個分段有多少行

[cpp] view plaincopy

  1. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {  

  2.         NSArray *cities = [[provinces objectAtIndex:section]objectForKey:@"Citys"];  

  3.     return [cities count];  

  4. }  

  •    這個方法用來告訴某個分組的某一行是什麼數據,返回一個UITableViewCell

[cpp] view plaincopy

  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  

  2.     NSUInteger section = [indexPath section];   

  3.     NSUInteger row = [indexPath row];   

  4.       

  5.       

  6.     NSArray *cities = [[provinces objectAtIndex:section]objectForKey:@"Citys"] ;  

  7.       

  8.       

  9.     static NSString *GroupedTableIdentifier = @"cell";   

  10.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:   

  11.                              GroupedTableIdentifier];   

  12.     if (cell == nil) {   

  13.         cell = [[UITableViewCell alloc]   

  14.                 initWithStyle:UITableViewCellStyleDefault   

  15.                 reuseIdentifier:GroupedTableIdentifier];   

  16.     }   

  17.       

  18.     //給Label附上城市名稱  key 爲:C_Name  

  19.     cell.textLabel.text = [[cities objectAtIndex:row] objectForKey:@"C_Name"];   

  20.     return cell;   

  21. }  

  • 這個方法用來告訴表格第section分組的名稱 

[cpp] view plaincopy

  1. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {  

  2.     NSString *provincName = [[provinces objectAtIndex:section] objectForKey:@"p_Name"];  

  3.     return provincName;   

  4. }  



  • 重點介紹下這個方法:

[cpp] view plaincopy

  1. - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {  

  2.     //返回省份的數組  

  3.     NSMutableArray *array = [NSMutableArray arrayWithCapacity:35];  

  4.     for (NSDictionary *dict in provinces) {  

  5.         [array addObject:[dict objectForKey:@"p_Name"]];  

  6.     }  

  7.     return array;  

  8. }  

返回全部省份名稱的數組 ,經過點擊右邊的省份名稱能快速定位到這個省份的城市,也就是快速定位到這個section。

OK,運行。效果以下:



試試改爲plain樣式的分段TableView看看:



以上例子的所有


例子的代碼:http://download.csdn.net/detail/totogo2010/4361876

https://github.com/schelling/YcDemo/tree/master/TableViewGrouped

著做權聲明:本文由http://blog.csdn.net/totogo2010/原創,歡迎轉載分享。請尊重做者勞動,轉載時保留該聲明和做者博客連接,謝謝!

相關文章
相關標籤/搜索