iOS_10_tableView的简单使用_红楼十二钗
终于效果图:
方式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的简单使用_红楼十二钗的更多相关文章
- pytho简单爬虫_模拟登陆西电流量查询_实现一键查询自己的校园网流量
闲来无事,由于校园内网络是限流量的,查询流量很是频繁,于是萌生了写一个本地脚本进行一键查询自己的剩余流量. 整个部分可以分为三个过程进行: 对登陆时http协议进行分析 利用python进行相关的模拟 ...
- Java使用poi对Execl简单操作_总结
poi是Apache组织给开发者提供一套操作office(Execl,Word,PowerPoint)等Java API,开发者通过Poi API可以快速的操作office办公软件,以上3篇博文只是一 ...
- centos 6.5 下 nginx 简单优化_虚拟主机_负载均衡
# 用了nginx for win很久,安装也是超级简单.# 还是用一下linux版的吧.环境是centos 6.5 x64 # 安装开始: # 先安装依赖 yum install gcc-c++ y ...
- 3_Jsp标签_简单标签_防盗链和转义标签的实现
一概念 1防盗链 在HTTP协议中,有一个表头字段叫referer,采用URL的格式来表示从哪儿链接到当前的网页或文件,通过referer,网站可以检测目标网页访问的来源网页.有了referer跟踪来 ...
- iOS_12_tableViewCell的删除更新_红楼梦
终于效果图: Girl.h // // Girl.h // 12_tableView的增删改 // // Created by beyond on 14-7-27. // Copyright (c) ...
- QT_4_QpushButton的简单使用_对象树
QpushButton的简单使用 1.1 按钮的创建 QPushButton *btn = new QPushButton; 1.2 btn -> setParent(this);设置父窗口 1 ...
- SignalR简单实用_转自:https://www.cnblogs.com/humble/p/3851205.html
一.指定通信方式 建立一个通讯方式需要一定的时间和客户机/服务器资源.如果客户机的功能是已知的,那么通信方式在客户端连接开始的时候就可以指定.下面的代码片段演示了使用AJAX长轮询方式来启动一个连接, ...
- JSON的简单使用_向前台发送JSON数据
转自:http://www.cnblogs.com/digdeep/p/5574366.html 1.前台页面 <%@ page language="java" conten ...
- JSON的简单使用_解析前台传来的JSON数据
package cn.rocker.json; import org.junit.Test; import net.sf.json.JSONArray; import net.sf.json.JSON ...
随机推荐
- C# - 委托_求定积分通用方法
代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst ...
- 【linux驱动笔记】linux模块机制浅析
1. 模块module 操作系统分微内核和宏内核,微内核优点,可以使操作系统仅作很少的事,其它事情如网络处理等都作为应用程序来实现,微内核精简的同时,必然带来性能的下降.而linux的宏内核设 ...
- c语言,变长数组
下面这个结构体,可以在malloc的时候指定数据data的长度,这样的形式就是变长数组:typedef struct{ int data_len; char data[0];//或char data[ ...
- 使用VS2012主题插件创建自己的主题
上篇文章讲了如何更换VS2012的主题,具体内容请参考:Vistual Studio 2012更换皮肤.可是上面的步骤仅仅让我们可选择的主题是增多了,我们可不可以自己创建自己的主题呢? 答案是肯定的, ...
- Silverlight技术调查(2)——跨域访问
原文 Silverlight技术调查(2)——跨域访问 此调查web容器采用的是Tomcat,若允许所有域访问,只需在webapps下的根应用ROOT中,加入配置文件:clientaccesspoli ...
- 嵌入式ntp服务器的移植
一.交叉编译 1.官网下载http://www.ntp.org/点击download选项页 我的版本是ntp-4.2.6p5.tar.gz 2.解压 tar -zxvf ntp-4.2.6p5.tar ...
- ArcGIS 10.3 for Desktop新特性介绍
ArcGIS 10.3是一个完整公布的ArcGIS平台,它包含新的产品(ArcGIS Pro),针对10.2版本号产品进行了功能增强和稳定性的改进. ArcGIS 10.3 for Server新特性 ...
- 原始的js代码和jquery对比
Even a task as simple as this can be complicated without jQuery at our disposal. In plain JavaScript ...
- N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法
回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...
- Servlet的学习之Response响应对象(2)
本篇接上一篇<Servlet的学习之Response响应对象(1)>,继续从HttpServletResponse响应对象来介绍其方法和功能. 使用setHeader方法结合HTTP协议的 ...