带分区的UITableView的创建(省市区字典)

创建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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章