簡介:上篇作了Table View的一些介紹 ,還作了一個TableView 的Plain樣式的例子,這篇咱們學習Grouped樣式表的例子,還有用到前面讀取Plist的知識,把Plist文件中的數據讀取出來,放到Table view裏展現出來。這裏把全國30多個省份的城市,都列出來了,plist文件裏還有城市的行政區,不過這裏只列出省份和城市就ok了。效果圖以下:
![](http://static.javashuo.com/static/loading.gif)
那麼開始吧。git
一、新建項目github
新的一個名稱爲TableViewGrouped的Single View Application項目,打開項目的xib文件,拖拽TableView控件到xib文件中,擺正位置。數組
二、給新建的TableView找到他的歸屬學習
選中新添的TableView ,Connection Inspector,找到delegate和datasource,從它們右邊的圓圈拉線到Files Owner圖標上,參考上篇的第3步:
atom
![](http://static.javashuo.com/static/loading.gif)
三、設置Table View的屬性爲Grouped樣式spa
![](http://static.javashuo.com/static/loading.gif)
四、導入plist文件.net
從其餘文件夾導入Provineces.plist文件,這個文件我會傳到源代碼裏,你們都能方便使用了,包括全國30個省份和城市,還有城市的區也有。blog
![](http://static.javashuo.com/static/loading.gif)
五、添加.h .m的實現代碼。ci
.h文件添加一個propertyget
- #import <UIKit/UIKit.h>
-
- @interface ViewController : UIViewController
- @property (strong, nonatomic) NSArray *provinces;
- @end
第一步從Plist讀取出數據,第二步給Table添加數據。
在viewDidLoad讀取Plist,plist是個array類型的,因此使用Array讀取。
.m文件的實現。
- @implementation ViewController
- @synthesize provinces;
-
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- NSBundle *bundle = [NSBundle mainBundle];
- NSString *plistPath = [bundle pathForResource:@"Provineces" ofType:@"plist"];
- NSMutableArray *array=[[NSMutableArray alloc] initWithContentsOfFile:plistPath];
- self.provinces = array;
-
- }
實現TableView表格部分,下面這些方法看方法名就能大概明白意思。
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- return [provinces count];
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- NSArray *cities = [[provinces objectAtIndex:section]objectForKey:@"Citys"];
- return [cities count];
- }
- 這個方法用來告訴某個分組的某一行是什麼數據,返回一個UITableViewCell
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- NSUInteger section = [indexPath section];
- NSUInteger row = [indexPath row];
-
-
- NSArray *cities = [[provinces objectAtIndex:section]objectForKey:@"Citys"] ;
-
-
- static NSString *GroupedTableIdentifier = @"cell";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
- GroupedTableIdentifier];
- if (cell == nil) {
- cell = [[UITableViewCell alloc]
- initWithStyle:UITableViewCellStyleDefault
- reuseIdentifier:GroupedTableIdentifier];
- }
-
- //給Label附上城市名稱 key 爲:C_Name
- cell.textLabel.text = [[cities objectAtIndex:row] objectForKey:@"C_Name"];
- return cell;
- }
- - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
- NSString *provincName = [[provinces objectAtIndex:section] objectForKey:@"p_Name"];
- return provincName;
- }
- - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
- //返回省份的數組
- NSMutableArray *array = [NSMutableArray arrayWithCapacity:35];
- for (NSDictionary *dict in provinces) {
- [array addObject:[dict objectForKey:@"p_Name"]];
- }
- return array;
- }
返回全部省份名稱的數組 ,經過點擊右邊的省份名稱能快速定位到這個省份的城市,也就是快速定位到這個section。
OK,運行。效果以下:
![](http://static.javashuo.com/static/loading.gif)
試試改爲plain樣式的分段TableView看看:
![](http://static.javashuo.com/static/loading.gif)
以上例子的所有
例子的代碼:http://download.csdn.net/detail/totogo2010/4361876
https://github.com/schelling/YcDemo/tree/master/TableViewGrouped