昨天用字典构筑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. pouchdb-all-dbs插件

    pouchdb-all-dbs插件 用来获取所有数据库的名字列表 https://github.com/nolanlawson/pouchdb-all-dbs 使用方法 1.引入js文件(顺序如下) ...

  2. 前端需要注意的seo

    1 合理的title ,description ,keyswords 搜索引擎对这三项的权重逐渐减小,title 强调重点即可 ,重要的关键字不要超过两次,而且要靠前. 2 不同的tilte要有所不同 ...

  3. CharacterEncodingFilter -处理字符格式

    package com.pb.news.web.filter; import java.io.IOException;import javax.servlet.Filter;import javax. ...

  4. readSerializableObj

    package JBJADV003;import java.io.*;public class readSerializableObj { public static void main(String ...

  5. 利用powershell反弹shell到metasploit

    一.使用msfvenom生成PS1文件: msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST= -f psh-reflection >.p ...

  6. Django学习(七)---添加新文章页面

    在template中添加add_article.html页面 (form  input)请求方法使用post 这个页面涉及到了两个响应函数 1)显示页面的响应函数  2)表单提交的响应函数 add_a ...

  7. 求n个逆元的O(n)算法

    它的推导过程如下,设,那么 对上式两边同时除,进一步得到 再把和替换掉,最终得到 初始化,这样就可以通过递推法求出模奇素数的所有逆元了. 转自  http://blog.csdn.net/acdrea ...

  8. 简单介绍Struts2

    Struts2概述 Struts2虽然是Struts1的基础上发展起来的,但是实质上是以WebWork框架为核心,为传统的Struts1注入了WebWork的设计理念,统一了Struts1和WebWo ...

  9. 【学习笔记】C# 封装和继承

    封装 封装是实现面向对象程序设计的第一步 封装就是将数据.方法等集合在一个个单元中,我们称之为类 封装的意义在于保护代码/数据,屏蔽复杂性 继承 继承是所有面向对象语言不可缺少的部分 继承是为了实现类 ...

  10. Jenkins 的svn插件下载的代码不是最新代码的问题

    项目组使用Jenkins做自动化的每日编译和单元测试.经常发现,当提交完代码后,在Jenkins的每日编译代码还是旧代码,刚提交的代码并没有check out出来. 后来发现Jenkins服务器的时间 ...