簡介:上篇作了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,找到delegate和datasource,從它們右邊的圓圈拉線到Files Owner圖標上,參考上篇的第3步:spa
三、設置Table View的屬性爲Grouped樣式.net
四、導入plist文件orm
從其餘文件夾導入Provineces.plist文件,這個文件我會傳到源代碼裏,你們都能方便使用了,包括全國30個省份和城市,還有城市的區也有。blog
五、添加.h .m的實現代碼。ci
.h文件添加一個property
[cpp] view plaincopy
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (strong, nonatomic) NSArray *provinces;
@end
第一步從Plist讀取出數據,第二步給Table添加數據。
在viewDidLoad讀取Plist,plist是個array類型的,因此使用Array讀取。
.m文件的實現。
[cpp] view plaincopy
@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表格部分,下面這些方法看方法名就能大概明白意思。
這個方法用來告訴表格有幾個分組
[cpp] view plaincopy
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [provinces count];
}
這個方法告訴表格第section個分段有多少行
[cpp] view plaincopy
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSArray *cities = [[provinces objectAtIndex:section]objectForKey:@"Citys"];
return [cities count];
}
這個方法用來告訴某個分組的某一行是什麼數據,返回一個UITableViewCell
[cpp] view plaincopy
- (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;
}
這個方法用來告訴表格第section分組的名稱
[cpp] view plaincopy
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString *provincName = [[provinces objectAtIndex:section] objectForKey:@"p_Name"];
return provincName;
}
重點介紹下這個方法:
[cpp] view plaincopy
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
//返回省份的數組
NSMutableArray *array = [NSMutableArray arrayWithCapacity:35];
for (NSDictionary *dict in provinces) {
[array addObject:[dict objectForKey:@"p_Name"]];
}
return array;
}
返回全部省份名稱的數組 ,經過點擊右邊的省份名稱能快速定位到這個省份的城市,也就是快速定位到這個section。
OK,運行。效果以下:
試試改爲plain樣式的分段TableView看看:
以上例子的所有
例子的代碼:http://download.csdn.net/detail/totogo2010/4361876
https://github.com/schelling/YcDemo/tree/master/TableViewGrouped
著做權聲明:本文由http://blog.csdn.net/totogo2010/原創,歡迎轉載分享。請尊重做者勞動,轉載時保留該聲明和做者博客連接,謝謝!