昨天用字典构筑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

一下是效果图

重构tableview!的更多相关文章

  1. ReactiveCocoa代码实践之-RAC网络请求重构

    前言 RAC相比以往的开发模式主要有以下优点:提供了统一的消息传递机制:提供了多种奇妙且高效的信号操作方法:配合MVVM设计模式和RAC宏绑定减少多端依赖. RAC的理论知识非常深厚,包含有FRP,高 ...

  2. 代码重构 & 常用设计模式

    代码重构 重构目的 相同的代码最好只出现一次 主次方法 主方法 只包含实现完整逻辑的子方法 思维清楚,便于阅读 次方法 实现具体逻辑功能 测试通过后,后续几乎不用维护 重构的步骤 1  新建一个方法 ...

  3. IOS详解TableView——选项抽屉(天猫商品列表)

    在之前的有篇文章讲述了利用HeaderView来写类似QQ好友列表的表视图. 这里写的天猫抽屉其实也可以用该方法实现,具体到细节每个人也有所不同.这里采用的是点击cell对cell进行运动处理以展开“ ...

  4. iOS有关横向TableView的东西

    之前看到Apple store里面有横向的tableview,当然也有可能是collectionview啦. 尤其是项目中只有一条那么需要横向滑动的东西,就没有必要使用庞大的collectionvie ...

  5. 记一次.NET代码重构

    好久没写代码了,终于好不容易接到了开发任务,一看时间还挺充足的,我就慢慢整吧,若是遇上赶进度,基本上直接是功能优先,完全不考虑设计.你可以认为我完全没有追求,当身后有鞭子使劲赶的时候,神马设计都是浮云 ...

  6. CSharpGL(17)重构CSharpGL

    CSharpGL(17)重构CSharpGL CSharpGL用起来我自己都觉得繁琐了,这是到了重构的时候. 下载 CSharpGL已在GitHub开源,欢迎对OpenGL有兴趣的同学加入(https ...

  7. ASP.NET MVC5+EF6+EasyUI 后台管理系统(58)-DAL层重构

    系列目录 前言:这是对本文系统一次重要的革新,很久就想要重构数据访问层了,数据访问层重复代码太多.主要集中增删该查每个模块都有,所以本次是为封装相同接口方法 如果你想了解怎么重构普通的接口DAL层请查 ...

  8. ASP.NET MVC5+EF6+EasyUI 后台管理系统(59)-BLL层重构

    系列目录 前言:  这应该是本系统最后一次重构,将重构BLL层和Model层.来完全取代代码生成器生成的BLL层和DAL层.完全废掉了代码生成器的DAL,BLL,MODEL层.  全自动生成增,删,改 ...

  9. 原生JS实现全屏切换以及导航栏滑动隐藏及显示——重构前

    思路分析: 向后滚动鼠标滚轮,页面向下全屏切换:向前滚动滚轮,页面向上全屏切换.切换过程为动画效果. 第一屏时,导航栏固定在页面顶部,切换到第二屏时,导航条向左滑动隐藏.切换回第一屏时,导航栏向右滑动 ...

随机推荐

  1. Spring MVC 项目搭建 -4- spring security-添加自定义登录页面

    Spring MVC 项目搭建 -4- spring security-添加自定义登录页面 修改配置文件 <!--spring-sample-security.xml--> <!-- ...

  2. 解决java.lang.NumberFormatException: For input string: "id"

    今天,项目突然报"java.lang.NumberFormatException:For input string:"id"",项目框架是spring,spri ...

  3. RabbitMQ安装|使用|概念|Golang开发

    搬砖的陈大师版权所有,转载请注明:http://www.lenggirl.com/tool/RabbitMQ.html 手册:http://www.rabbitmq.com/getstarted.ht ...

  4. Hadoop出现的错误及处理

    1.local host is: (unknown); destination host is: "yun-ubuntu":8031; 原因:yun-ubuntu这个host 并不 ...

  5. list-列表功能介绍

    叨逼叨:列表是可变的,针对列表的改变,变得是列表本身,和字符串区别开来 #1.追加 # name = ['alex','eric','seven','qiqi'] # v = name.append( ...

  6. [问题解决]linux sudo xxx:command not found

    题外话 软件的安装在linux下主要分为两种.一种是通过包管理器例如ubuntu的apt-get xxx,另一种是自己手动安装.通过包管理器安装的,基本开箱即用,无需配置,但是存在一个问题,有时候无法 ...

  7. 微信小程序+OLAMI(欧拉蜜)自然语言API接口制作智能查询工具--快递、聊天、日历等

    微信小程序最近比较热门,再加上自然语义理解也越来越被人关注,于是我想赶赶潮流,做一个小程序试试.想来想去快递查询应该是一种比较普遍的需求. 如果你也在通过自然语言接口做点什么,希望我的这篇博客能帮到你 ...

  8. tensorflow tanh应用

    1.tanh()函数 tanh是双曲函数中的一个,tanh()为双曲正切. 双曲正切函数的导数公式:   2.tensorflow tanh()例子 import tensorflow as tf i ...

  9. JAVA 笔记 ClassLoader.getResourceAsStream() 与 Class.getResourceAsStream()的区别

       Class.getResourceAsStream() 会指定要加载的资源路径与当前类所在包的路径一致. 例如你写了一个MyTest类在包com.test.mycode 下,那么MyTest.c ...

  10. 如何将 Microsoft Bot Framework 机器人部署以及网页应用

    <Bot Framework>是微软开发的一款可让任何人制作自己的聊天机器人.该工具可以理解自然语言并对图片进行分析等,初期开放了 22 个可集成到应用的 API. 首先说到Bot大家的第 ...