昨天用字典构筑tableview简直愚蠢!不过蠢过才知道如何写出好的代码。下面将用模型重构tableview!
----------------------------------------------------------------------------------------------------------
step1:创建一个类,封装属性,并用创建于一个类方法便于我们一次性实例化对象
#import <UIKit/UIKit.h> @interface Province : UIViewController //NSString一般用copy,控件一般用weak,其他用strong @property (nonatomic,copy) NSString * header; @property (nonatomic,copy) NSString * footer; @property (nonatomic,strong) NSArray *cities; + (id) provinceWithHeader:(NSString *)header footer:(NSString *)footer cities:(NSArray *)cities; @end
#import "Province.h" @interface Province () @end @implementation Province - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ + (Province *)provinceWithHeader:(NSString *)header footer:(NSString *)footer cities:(NSArray *)cities { Province * province = [[Province alloc]init]; province.header = header; province.footer = footer; province.cities = cities; return province; } @end
step2:实例化对象创建tableview
#import "ViewController.h" #import "Province.h" @interface ViewController ()<UITableViewDataSource,UITableViewDelegate> { NSArray * _allCities; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UITableView * tableview = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped]; tableview.dataSource = self; [self.view addSubview:tableview]; Province *gd = [Province provinceWithHeader:@"广东" footer:@"广东可以的" cities: @[@"广州",@"东莞",@"佛山"]]; Province * zj = [Province provinceWithHeader:@"浙江" footer:@"浙江好" cities:@[@"杭州",@"台州",@"宁波",@"温州"]]; Province * js = [Province provinceWithHeader:@"江苏" footer:@"江苏可以的" cities:@[@"南京",@"苏州",@"无锡"]]; _allCities = @[gd,zj,js]; } #pragma mark 一共有多少组 section==组 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return _allCities.count; } #pragma mark 每组有多少行 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSArray *arr = [_allCities[section] cities]; return arr.count; } #pragma mark 为cell添加内容 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; NSArray *arr = [_allCities[indexPath.section] cities]; cell.textLabel.text = arr[indexPath.row]; return cell; } #pragma mark 添加表头 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return [_allCities[section] header]; } #pragma mark 添加表尾 - (NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { return [_allCities[section] footer]; } #pragma mark 添加索引 - (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView { //创建可变数组 NSMutableArray *arr = [NSMutableArray array]; //遍历数组里面的province对象 for (Province * p in _allCities) { [arr addObject:p.header];//取出对象中的header属性 } return arr; } @end
一下是效果图