带分区的省市区

带分区的省市区

import “MainViewController.h”

define WIDTH self.view.frame.size.width

define HEIGHT self.view.frame.size.height

@interface MainViewController ()

warning 如果在初始化方法里使用self.view,此时还没有创建self.view,系统会自动调用loadview,创建一个self.view,从而改变VC的运行流程,所以我们只在初始化方法里初始化容器等数据部分,不能建视图

-(instancetype)initWithNibName:(NSString )nibNameOrNil bundle:(NSBundle )nibBundleOrNil{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[self createData];
}
return self;
}

-(void)createData{
NSString *path=@”/Users/dlios/Desktop/内容/UI/UI10_带分区的省市区/UI10_带分区的省市区/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 setObject:temp forKey:@”proName”];
NSMutableArray *cityArr=[NSMutableArray array];
[proDic setObject:cityArr forKey:@”cityArr”];
[self.proArr addObject:proDic];
}else if ([temp hasPrefix:@” “]&&![temp hasPrefix:@” “]){
NSMutableDictionary *cityDic=[NSMutableDictionary dictionary];
[cityDic setObject:temp forKey:@”cityName”];
NSMutableArray *zoneArr=[NSMutableArray array];
[cityDic setObject:zoneArr forKey:@”zoneArr”];
NSMutableDictionary *proDic=[self.proArr lastObject];
NSMutableArray *cityArr=proDic[@”cityArr”];
[cityArr addObject:cityDic];
}else{
NSMutableDictionary *proDic=[self.proArr lastObject];
NSMutableArray *cityArr=proDic[@”cityArr”];
NSMutableDictionary *cityDic=[cityArr lastObject];
NSMutableArray *zoneArr=cityDic[@”zoneArr”];
[zoneArr addObject:temp];
}
}

}

  • (void)dealloc
    {
    [_cityArr release];
    [_proArr release];
    [_tableView release];
    [super dealloc];
    }

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor=[UIColor redColor];
    self.navigationController.navigationBar.translucent=NO;
    self.title=@”带分区的省市区”;

    self.tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 667-64) style:(UITableViewStylePlain)];
    [self.view addSubview:self.tableView];
    [self.tableView release];
    self.tableView.backgroundColor=[UIColor redColor];
    self.tableView.rowHeight=40;
    self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    self.tableView.dataSource=self;
    self.tableView.delegate=self;

}

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    // 找省对应的市数组
    NSMutableDictionary *proDic=self.proArr[section];
    self.cityArr=proDic[@”cityArr”];
    return self.cityArr.count;
    }

// 分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return self.proArr.count;
}

-(NSString )tableView:(UITableView )tableView titleForHeaderInSection:(NSInteger)section{
return self.proArr[section][@”proName”];
}

  • (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *reuse=@”reuse”;
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
    cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
    }
    // 根据分区先找到省字典
    NSMutableDictionary *proDic=self.proArr[indexPath.section];
    self.cityArr=proDic[@”cityArr”];
    NSMutableDictionary *cityDic=self.cityArr[indexPath.row];
    cell.textLabel.text=cityDic[@”cityName”];
    return cell;
    }

-(UIView )tableView:(UITableView )tableView viewForHeaderInSection:(NSInteger)section{
UIView *newView=[[[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 20)] autorelease];
newView.backgroundColor=[UIColor yellowColor];

UILabel *leftLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 20)];
[newView addSubview:leftLabel];
[leftLabel release];
leftLabel.text=self.proArr[section][@"proName"];

UIButton *rightButton=[UIButton buttonWithType:(UIButtonTypeCustom)];
rightButton.frame=CGRectMake(300, 0, 50, 20);
[newView addSubview:rightButton];
[rightButton setTitle:@"更多" forState:UIControlStateNormal];
[rightButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[rightButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
return newView;

}

-(void)click:(UIButton *)button{
NSLog(@”点死你”);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值