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 ...
随机推荐
- UITabBarController中自定义UITabBar
1.创建多个视图控制器,放如UITabBarController中 AViewController *aa = [[AViewController alloc] init]; UINavigation ...
- js轮盘抽奖
js轮盘抽奖 需求:实现中奖是否可控 思路:通过旋转角度来实现轮盘转动,根据角度来确定是否中奖 window.onload = function(){ var oTurn = document.get ...
- QWidget类中默认是忽略inputMethodEvent事件(要获取输入的内容就必须使用这个事件)
因为项目的需要以及主管的要求,准备将工程移植到Qt中,这样就可以比较容易的实现跨平台了.因为之前工程是在windows下开发的,第一个平台又是mobile所以除了底层框架之外其他的都是使用的windo ...
- WSGI详解
WSGI接口 了解了HTTP协议和HTML文档,我们其实就明白了一个Web应用的本质就是: 浏览器发送一个HTTP请求: 服务器收到请求,生成一个HTML文档: 服务器把HTML文档作为HTTP响应的 ...
- MySQL存储引擎:InnoDB和MyISAM的差别/优劣评价/评测/性能测试
InnoDB和MyISAM简介 MyISAM:这个是默认类型,它是基于传统的ISAM类型,ISAM是Indexed Sequential Access Method (有索引的 顺序访问方法) 的缩写 ...
- C#利用Lambda和Expression实现数据的动态绑定
在程序开发过程中,有时为了让数据能够实时更新,我们会采用数据绑定来实现. 一般我们数据绑定时我们是这样写的 public class Helper : INotifyPropertyChanged { ...
- lucene做简单的文件索引
package com.mylucene; import java.io.File; import java.io.FileReader; import java.io.IOException; im ...
- Swift - 实现点击UITableView单元格时自动展开单元格
下面是一个列表单元格cell的折叠展开效果的demo.当点击单元格时会展开该单元格,便于显示一些详情什么的.点击其他单元格原来的会关闭,同时有动画效果. 效果如如下: 代码如下: 1 2 3 4 ...
- android ListView和GridView拖拽移位实现代码
关于ListView拖拽移动位置,想必大家并不陌生,比较不错的软件都用到如此功能了.如:搜狐,网易,百度等,但是相比来说还是百度的用户体验较好,不偏心了,下面看几个示例: 首先 ...
- 刚開始学习的人制作VMOS场效应管小功放
VMOS场效应管既有电子管的长处又有晶体管的长处,用它制作的功率放大器声音醇厚.甜美,动态范围大.频率响应好.因此近年来在音响设备中得到了广泛应用. 大功率的场效应管功率放大器.电.路比較复杂.制作和 ...