终于效果图:

方式1,用字典数组

BeyondViewController.h

//
// BeyondViewController.h
// 10_tableView
//
// Created by beyond on 14-7-25.
// Copyright (c) 2014年 com.beyond. All rights reserved.
// #import <UIKit/UIKit.h> @interface BeyondViewController : UIViewController @end

BeyondViewController.m

//
// BeyondViewController.m
// 10_tableView
//
// Created by beyond on 14-7-25.
// Copyright (c) 2014年 com.beyond. All rights reserved.
// #import "BeyondViewController.h"
#define kHeader @"header"
#define kFooter @"footer"
#define kGirlsArr @"girls" @interface BeyondViewController ()<UITableViewDataSource>
{
// 假数据
NSArray *_array;
}
@end @implementation BeyondViewController - (void)viewDidLoad
{
[super viewDidLoad];
// 假数据 方式1 用字典
_array = @[ @{kHeader: @"十二钗正冊",
kGirlsArr:@[@"林黛玉",@"薛宝钗",@"贾元春",@"贾探春",@"史湘云",@"妙玉",@"贾迎春",@"贾惜春",@"王熙凤",@"贾巧姐",@"李纨",@"秦可卿"],
kFooter:@"红楼梦"
},
@{kHeader: @"十二钗副冊",
kGirlsArr:@[@"香菱",@"薛宝琴",@"尤二姐",@"尤三姐",@"邢岫烟",@"李纹",@"李绮",@"夏金桂",@"秋桐",@"小红",@"龄官",@"娇杏"],
kFooter:@"红楼梦"
},
@{kHeader: @"十二钗又副冊",
kGirlsArr:@[@"晴雯",@"袭人",@"平儿",@"鸳鸯",@"紫鹃",@"莺儿",@"玉钏",@"金钏",@"彩云",@"司棋",@"芳官",@"麝月"],
kFooter:@"红楼梦"
}
];
// 假数据 方式2 用类封装 // 样式仅仅有两种 Grouped Plain
UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
// 数据源
tableView.dataSource = self;
// 加入到self.view
[self.view addSubview:tableView];
}
// 数据源方法,特例,重要~ 一共同拥有多少个分组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return _array.count;
}
// 数据源方法,每一组,有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// 返回数组中相应的字典的长度
return [[_array[section] objectForKey:kGirlsArr] count];
}
// 数据源方法,每一组的每一行应该显示怎么的界面(含封装的数据),重点!!!必须实现否则,Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = @"Beyond";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
// 假设池中没取到,则又一次生成一个cell
/*
cell的4种样式:
1,default 左图右文字
2,subtitle 左图 上文字大 下文字小
3,value 1 左图 左文字大 右文字小
3,value 2 恶心 左文字小 右文字大
*/
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
// 设置cell中独一无二的内容
cell.textLabel.text = [_array[indexPath.section] objectForKey:kGirlsArr][indexPath.row];
// 返回cell
return cell;
}
// 数据源方法,组的开头显示什么标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [_array[section] objectForKey:kHeader];
}
// 数据源方法,,组的最后显示什么标题
//- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
//{
// return [_array[section] objectForKey:kFooter];
//} @end

方式2,用类(model)取代数组中的字典

TwelveBeauties.h

//
// TwelveBeauties.h
// 10_tableView
//
// Created by beyond on 14-7-26.
// Copyright (c) 2014年 com.beyond. All rights reserved.
// #import <Foundation/Foundation.h>
// 相应 viewController.m中的成员数组中的一个成员 --> 字典
@interface TwelveBeauties : NSObject
// UI控件连线时用weak,字符串用copy,其它对象用strong
@property (nonatomic,copy) NSString *header;
@property (nonatomic,copy) NSString *footer;
@property (nonatomic,strong) NSArray *girls;
// 提供一个类方法,一个以类名开头的构造方法(返回id亦可)
+ (TwelveBeauties *)twelveBeautiesWithHeader:(NSString *)header footer:(NSString *)footer girls:(NSArray *)girls;
@end

TwelveBeauties.m

//
// TwelveBeauties.m
// 10_tableView
//
// Created by beyond on 14-7-26.
// Copyright (c) 2014年 com.beyond. All rights reserved.
// #import "TwelveBeauties.h" @implementation TwelveBeauties
// 提供一个类方法,一个以类名开头的构造方法(返回id亦可)
+ (TwelveBeauties *)twelveBeautiesWithHeader:(NSString *)header footer:(NSString *)footer girls:(NSArray *)girls
{
TwelveBeauties *twelveBeauties = [[TwelveBeauties alloc]init];
twelveBeauties.header = header;
twelveBeauties.footer = footer;
twelveBeauties.girls = girls;
return twelveBeauties;
}@end

BeyondViewController.m

//
// BeyondViewController.m
// 10_tableView
//
// Created by beyond on 14-7-25.
// Copyright (c) 2014年 com.beyond. All rights reserved.
// #import "BeyondViewController.h"
#import "TwelveBeauties.h" @interface BeyondViewController ()<UITableViewDataSource>
{
// 假数据
NSArray *_array;
}
@end @implementation BeyondViewController - (void)viewDidLoad
{
[super viewDidLoad];
// 假数据 方式1 用字典
/*
_array = @[ @{kHeader: @"十二钗正冊",
kGirlsArr:@[@"林黛玉",@"薛宝钗",@"贾元春",@"贾探春",@"史湘云",@"妙玉",@"贾迎春",@"贾惜春",@"王熙凤",@"贾巧姐",@"李纨",@"秦可卿"],
kFooter:@"红楼梦"
},
@{kHeader: @"十二钗副冊",
kGirlsArr:@[@"香菱",@"薛宝琴",@"尤二姐",@"尤三姐",@"邢岫烟",@"李纹",@"李绮",@"夏金桂",@"秋桐",@"小红",@"龄官",@"娇杏"],
kFooter:@"红楼梦"
},
@{kHeader: @"十二钗又副冊",
kGirlsArr:@[@"晴雯",@"袭人",@"平儿",@"鸳鸯",@"紫鹃",@"莺儿",@"玉钏",@"金钏",@"彩云",@"司棋",@"芳官",@"麝月"],
kFooter:@"红楼梦"
}
];
*/
// 假数据 方式2 用类封装后
_array = @[
[TwelveBeauties twelveBeautiesWithHeader:@"十二钗正冊" footer:@"红楼梦" girls:@[">@"林黛玉",@"薛宝钗",@"贾元春",@"贾探春",@"史湘云",@"妙玉",@"贾迎春",@"贾惜春",@"王熙凤",@"贾巧姐",@"李纨",@"秦可卿"]],
[TwelveBeauties twelveBeautiesWithHeader:@"十二钗副冊" footer:@"红楼梦" girls:@[@"香菱",@"薛宝琴",@"尤二姐",@"尤三姐",@"邢岫烟",@"李纹",@"李绮",@"夏金桂",@"秋桐",@"小红",@"龄官",@"娇杏"]],
[TwelveBeauties twelveBeautiesWithHeader:@"十二钗又副冊" footer:@"红楼梦" girls:@[@"晴雯",@"袭人",@"平儿",@"鸳鸯",@"紫鹃",@"莺儿",@"玉钏",@"金钏",@"彩云",@"司棋",@"芳官",@"麝月"]]
];
// 样式仅仅有两种 Grouped Plain
UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
// 数据源
tableView.dataSource = self;
// 加入到self.view
[self.view addSubview:tableView];
}
// 数据源方法,特例,重要~ 一共同拥有多少个分组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return _array.count;
}
// 数据源方法,每一组,有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// 返回数组中相应的字典的长度
// return [[_array[section] objectForKey:kGirlsArr] count]; // 用数据模型封装后
TwelveBeauties *twelveBeauties = _array[section];
return twelveBeauties.girls.count;
}
// 数据源方法,每一组的每一行应该显示怎么的界面(含封装的数据),重点!!!必须实现否则,Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = @"Beyond";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
// 假设池中没取到,则又一次生成一个cell
/*
cell的4种样式:
1,default 左图右文字
2,subtitle 左图 上文字大 下文字小
3,value 1 左图 左文字大 右文字小
3,value 2 恶心 左文字小 右文字大
*/
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
// 设置cell中独一无二的内容,字典封装假数据
//cell.textLabel.text = [_array[indexPath.section] objectForKey:kGirlsArr][indexPath.row]; // 用数据模型封装后
TwelveBeauties *twelveBeauties = _array[indexPath.section];
cell.textLabel.text = [twelveBeauties.girls objectAtIndex:indexPath.row] ;
// 返回cell
return cell;
}
// 数据源方法,组的开头显示什么标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
// 字典封装假数据
// return [_array[section] objectForKey:kHeader]; // 用数据模型封装后
TwelveBeauties *twelveBeauties = _array[section];
return twelveBeauties.header;
}
// 数据源方法,组的索引的标题(通讯录最右边的竖条)
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
// 用KVC \ KVO 能够一句代码实现
return @[@"正冊",@"副冊",@"又副冊"] ;
// NSMutableArray *array = [NSMutableArray array];
// for (TwelveBeauties *tb in _array) {
// [array addObject:tb.header];
// }
// NSLog(@"%@",array);
// return array; } @end

iOS_10_tableView的简单使用_红楼十二钗的更多相关文章

  1. pytho简单爬虫_模拟登陆西电流量查询_实现一键查询自己的校园网流量

    闲来无事,由于校园内网络是限流量的,查询流量很是频繁,于是萌生了写一个本地脚本进行一键查询自己的剩余流量. 整个部分可以分为三个过程进行: 对登陆时http协议进行分析 利用python进行相关的模拟 ...

  2. Java使用poi对Execl简单操作_总结

    poi是Apache组织给开发者提供一套操作office(Execl,Word,PowerPoint)等Java API,开发者通过Poi API可以快速的操作office办公软件,以上3篇博文只是一 ...

  3. centos 6.5 下 nginx 简单优化_虚拟主机_负载均衡

    # 用了nginx for win很久,安装也是超级简单.# 还是用一下linux版的吧.环境是centos 6.5 x64 # 安装开始: # 先安装依赖 yum install gcc-c++ y ...

  4. 3_Jsp标签_简单标签_防盗链和转义标签的实现

    一概念 1防盗链 在HTTP协议中,有一个表头字段叫referer,采用URL的格式来表示从哪儿链接到当前的网页或文件,通过referer,网站可以检测目标网页访问的来源网页.有了referer跟踪来 ...

  5. iOS_12_tableViewCell的删除更新_红楼梦

    终于效果图: Girl.h // // Girl.h // 12_tableView的增删改 // // Created by beyond on 14-7-27. // Copyright (c) ...

  6. QT_4_QpushButton的简单使用_对象树

    QpushButton的简单使用 1.1 按钮的创建 QPushButton *btn = new QPushButton; 1.2 btn -> setParent(this);设置父窗口 1 ...

  7. SignalR简单实用_转自:https://www.cnblogs.com/humble/p/3851205.html

    一.指定通信方式 建立一个通讯方式需要一定的时间和客户机/服务器资源.如果客户机的功能是已知的,那么通信方式在客户端连接开始的时候就可以指定.下面的代码片段演示了使用AJAX长轮询方式来启动一个连接, ...

  8. JSON的简单使用_向前台发送JSON数据

    转自:http://www.cnblogs.com/digdeep/p/5574366.html 1.前台页面 <%@ page language="java" conten ...

  9. JSON的简单使用_解析前台传来的JSON数据

    package cn.rocker.json; import org.junit.Test; import net.sf.json.JSONArray; import net.sf.json.JSON ...

随机推荐

  1. Lucene4.3入门

    辞职交接期间无聊看了一下搜索引擎,java社区比较火的当然是Lucene,想写一个简单的小例子,在网上找了些资料,不过都不是4.3的,自己看了一下. 下载地址:http://lucene.apache ...

  2. PHP - 防止非法调用页面

    这是在服务器内部: 首先定义一个常量 在调用页面的时候,检测是否存在此常量 如果存在,则调用 否则,做出提示. 创建常量: 创建常量的函数名称: define //创建一个常量,以便于页面调用,从主页 ...

  3. WCF技术剖析之六:为什么在基于ASP.NET应用寄宿(Hosting)下配置的BaseAddress无效

    原文:WCF技术剖析之六:为什么在基于ASP.NET应用寄宿(Hosting)下配置的BaseAddress无效 本篇文章来源于几天前一个朋友向我咨询的问题.问题是这样的,他说他采用ASP.NET应用 ...

  4. 基于visual Studio2013解决C语言竞赛题之1024求和

          题目 解决代码及点评 /* 已知有N个无规律的正整数,请编程序求出其中的素数并打印出能被5整除的数之积. */ #include <stdio.h> # ...

  5. 解决swfupload上传控件文件名中文乱码问题 三种方法 flash及最新版本11.8.800.168

    目前比较流行的是使用SWFUpload控件,这个控件的详细介绍可以参见官网http://demo.swfupload.org/v220/index.htm 在使用这个控件批量上传文件时发现中文文件名都 ...

  6. poj3278Catch That Cow(BFS)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 37094   Accepted: 11466 ...

  7. MapReduce调度与执行原理之作业提交

    前言 :本文旨在理清在Hadoop中一个MapReduce作业(Job)在提交到框架后的整个生命周期过程,权作总结和日后参考,如有问题,请不吝赐教.本文不涉及Hadoop的架构设计,如有兴趣请参考相关 ...

  8. inode结构体成员详解

    概述:inode译成中文就是索引节点,它用来存放档案及目录的基本信息,包含时间.档名.使用者及群组等.inode分为内存中的inode和文件系统中的inode,为了避免混淆,我们称前者为VFS ino ...

  9. Swift - iCloud存储介绍

    对于开发者而言,涉及iCloud存储的功能主要有两个: 一是 iCloud documnet storage,利用 iCloud 存储用户文件,比如保存一些用户在使用应用时生成的文件以及数据库文件等. ...

  10. iOS 使用UIBezierPath类实现随手画画板

    在上一篇文章中我介绍了 UIBezierPath类 介绍 ,下面这篇文章介绍一下如何通过这个类实现一个简单的随手画画板的简单程序demo,功能包括:划线(可以调整线条粗细,颜色),撤销笔画,回撤笔画, ...