創建MainViewController
1.初始化
代碼
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[self creatData];
}
return self;
}
2.創建省市區字典
代碼
-(void)creatData{
NSString *path=@"/Users/dllo/Desktop/上課內容/ui/UI0_帶分區的省市區/UI0_帶分區的省市區/area.txt";
NSString *str =[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSArray *strArr =[str componentsSeparatedByString:@"\n"];
self.proArr =[NSMutableArray array];
for (NSString *temp in strArr) {
if (![temp hasPrefix:@" " ]) {
NSMutableDictionary *proDic =[NSMutableDictionary dictionary];
[proDic setValue:temp forKey:@"proName"];
NSMutableArray *cityArr=[NSMutableArray array];
[proDic setValue:cityArr forKey:@"cityArr"];
[self.proArr addObject:proDic];
}else if([temp hasPrefix:@" "]&&![temp hasPrefix:@" "] ){
NSMutableDictionary *cityDic=[NSMutableDictionary dictionary];
[cityDic setValue:temp forKey:@"cityName"];
NSMutableArray *zoneArr=[NSMutableArray array];
[cityDic setValue:zoneArr forKey:@"zoneArr"];
NSMutableArray *cityArr=[[self.proArr lastObject] objectForKey:@"cityArr"];
[cityArr addObject:cityDic];
}else{
NSMutableArray *zoneArr =[[[[self.proArr lastObject] objectForKey:@"cityArr"] lastObject] objectForKey:@"zoneArr"];
[zoneArr addObject:temp];
}
}
}
3.之後在viewDidLoad中創建UITableView,不要忘記代理人的建立,和簽訂協議
代碼
self.view.backgroundColor =[UIColor yellowColor];
self.navigationController.navigationBar.translucent=NO;
UITableView *proTableView =[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64) style:UITableViewStylePlain];
[self.view addSubview:proTableView];
[proTableView release];
proTableView.delegate =self;
proTableView.dataSource =self;
4.#pragma mark 分區數 確定分區數(有多少個省,就有多少個分區)
代碼
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return self.proArr.count;
}
5.#pragma mark 每個分區的頭標題(也就是每個省的省名)
代碼:
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return self.proArr[section][@"proName"];
}
6.#pragma mark 每個分區裏的行數
每個分區的行數是每個省中市的個數
這個也是UITableView必須執行的第一個協議
代碼
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
// 省對應的市數組
NSMutableArray *cityArr =self.proArr[section][@"cityArr"];
return cityArr.count;
}
7.創建並設定cell (這是tableview第二個必須執行的協議)
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *reuse=@"reuse";
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:reuse];
if (!cell) {
cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
}
// 根據分區找到省字典
NSMutableArray *cityArr=[NSMutableArray array];
cityArr =self.proArr[indexPath.section][@"cityArr"];
cell.textLabel.text=cityArr[indexPath.row][@"cityName"];
return cell;
}
8.下面的方法可以對每個分區的標題行進行設置
代碼:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//創建一個newView,對標題欄的設置全在這個視圖上進行
UIView *newView =[[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 20)] autorelease];
// 設定背景顏色
[newView setBackgroundColor:[UIColor yellowColor]];
// 創建一個label 來寫標題
UILabel *textLabel =[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 70, 20)];
[newView addSubview:textLabel];
[textLabel release];
//把省名添加到Label中
textLabel.text=self.proArr[section][@"proName"];
//創建一個button添加在newView上
UIButton *button =[UIButton buttonWithType:UIButtonTypeSystem];
button.frame=CGRectMake(320, 10, 50, 20);
[button setTitle:@"更多..." forState:UIControlStateNormal];
[newView addSubview:button];
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
return newView;
}