【UITableView】02 - 数据分离 - 从新 - 学习笔记
来源:互联网 发布:java开发单片机 编辑:程序博客网 时间:2024/05/16 11:25
#import<Foundation/Foundation.h>
/**
* 第一步 -创建一个类
*
* 只在这个类中创建三个属性:头部文字 -尾部信息 -学员编号数组
*/
@interface SWStudent :NSObject
/**
* 头部文字
*/
@property (nonatomic,copy)NSString *title;
/**
* 尾部信息,注意 -不能使用description作为属性名
*/
@property (nonatomic,copy)NSString *desc;
/**
* 学员编号数组
*/
@property (nonatomic,strong)NSArray *students;
/**
* 第一步 -创建一个类
*
* 只在这个类中创建三个属性:头部文字 -尾部信息 -学员编号数组
*/
@interface SWStudent :NSObject
/**
* 头部文字
*/
@property (nonatomic,copy)NSString *title;
/**
* 尾部信息,注意 -不能使用description作为属性名
*/
@property (nonatomic,copy)NSString *desc;
/**
* 学员编号数组
*/
@property (nonatomic,strong)NSArray *students;
@end
#import<UIKit/UIKit.h>
@interface ViewController :UIViewController
/**
* 第五步
*
* 1. 先在storyBoard中把UITableView拖拽到storyboard上面
*
* 2. 在storyboard和这个类进行连线,名字写成tableView
*/
@property (weak,nonatomic)IBOutletUITableView *tableView;
@end
@interface ViewController :UIViewController
/**
* 第五步
*
* 1. 先在storyBoard中把UITableView拖拽到storyboard上面
*
* 2. 在storyboard和这个类进行连线,名字写成tableView
*/
@property (weak,nonatomic)IBOutletUITableView *tableView;
@end
//
// ViewController.m
// UITableView数据分离
//
// Created by mac mini on 15-3-12.
// Copyright (c) 2015年 SW. All rights reserved.
//
#import"ViewController.h"
#import "SWStudent.h" // 第二步:添加刚才创建的类
/**
* 第六步 -在storyboard中连线的时候选择的是什么这里写协议的时候就写成什么
*
* 两种协议:
*
* 1. <控件的名字 + DataSource> -定义的与数据有关的方法 - <UITableViewDataSource>
*
* 2. <控件的名字 + Delegate> -定义的与事件有关的方法(滚动,拖拽)通常用来监听控件的事件的 - <UITableViewDelegate>
*/
@interface ViewController () <UITableViewDataSource>
/**
* 第三步 -创建一个 -数据列表
*/
@property (nonatomic,strong)NSArray *dataList;
@end
@implementation ViewController
/**
* 第四步:数据列表 -懒加载
*
* @return 加载好的数据来显示
*/
- (NSArray *)dataList{
if (_dataList == nil) {
SWStudent *stu1 = [[SWStudentalloc]init];
stu1.title =@"2013.3.12";
stu1.desc =@"从新开始学习,第一遍";
//生成编号数组
NSMutableArray *arrayM1 = [NSMutableArrayarray];
for (int i = 0; i <10 ; i++) {
[arrayM1 addObject:[NSStringstringWithFormat:@"%@ - %04d",stu1.title,i]];
}
stu1.students = arrayM1 ;
SWStudent *stu2 = [[SWStudentalloc]init];
stu2.title =@"2013.3.12.20.59";
stu2.desc =@"网开科技";
//生成编号数组
NSMutableArray *arrayM2 = [NSMutableArrayarray];
for (int i = 0 ; i <5 ; i++) {
[arrayM2 addObject:[NSStringstringWithFormat:@"%@ - %03d",stu2.title,i]];
}
stu2.students = arrayM2;
SWStudent *stu3 = [[SWStudentalloc]init];
stu3.title =@"我是三组头";
stu3.desc =@"我是三组尾";
//创建一个生成编号的数组
NSMutableArray *arrayM3 = [NSMutableArrayarray];
for (int i = 0; i <9; i++) {
//把生成的字符串放到arrayM3中(太聪明了)
[arrayM3 addObject:[NSStringstringWithFormat:@"%@ - %02d",stu3.title,i]];
}
stu3.students = arrayM3;
SWStudent *stu4 = [[SWStudentalloc]init];
stu4.title =@"第四组头";
stu4.desc =@"第四组尾";
NSMutableArray *arrayM4 = [NSMutableArrayarray];
for (int i = 0; i <2; i++) {
[arrayM4 addObject:[NSStringstringWithFormat:@"%@ - %01d",stu4.title,i]];
}
stu4.students = arrayM4;
_dataList =@[stu2,stu1,stu3,stu4];
}
return_dataList ;
}
#pragma mark - UITableView的数据源方法<UITableViewDataSource>
// 如果没有实现,默认是1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
//返回数组的数据
returnself.dataList.count;
}
// 每个分组中得数据总数
// sction:分组的编号
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//取出 -数据列表 -中得分组的编号 -自己的理解
//取出数组中对应的学员信息 -教师的注释
//为什么用SWStudent这个自己创建的类来创建一个新的对象,并给这个对象赋值的是这个类中的数据列表的分组编号?
//这是因为:在下边返回每一行有多少行数的时候,需要调用的是“学员编号数组”的数组,而“学员编号数组”是在SWStudent这个自己创建的类里面创建的。
SWStudent *stu =self.dataList[section];
//返回 -对象stu -里面 -学员编号数组 -的个数(就是你在前边用SWStudent这个类中的student数组创建了几个数组,把这几个数组中有几行显示出来)
return stu.students.count;
}
// 告诉表格控件,每一行cell单元格的细节
// indexPath
// @property(nonatomic,readonly) NSInteger section; 分组
// @property(nonatomic,readonly) NSInteger row; 行
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//实例化TableViewCell时,使用initWithStyle方法来进行实例化
UITableViewCell *tableViewCell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:nil];
//取出indexPath对应的数据
// indexPath总共有2个属性,一个是section(分组),一个是row(行数)
//提取出来student对象的“学员编号数组”的分组信息
// SWStudent *perpor -中的perpor对象名称要是改写成其他的名称,那么后面的perpor.students[indexPath.section]和 perpor.students[indexPath.row]的perpor对象名称也要进行改变
SWStudent *perpor = perpor.students[indexPath.section];
//每一个cell中得text都是由以SWStudent为基础创建的对象perpor中得每一行来显示
tableViewCell.textLabel.text = perpor.students[indexPath.row];
return tableViewCell;
}
/**
* 返回每一个分组中的尾部信息
*/
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
// SWStudent *footerTitle = self.dataList[section];
//
// return footerTitle.desc;
//直接总数组中取出的对象是id类型,因为没有明确的类型,因此不能使用点语法(.语法),只能使用getter方法
//这里为什么不能用student数组?是因为用self调用的?
return [self.dataList[section]desc];
}
/**
* 返回每一个分组中得头部信息
*/
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
// SWStudent *headerTitle = self.dataList[section];
// return headerTitle.title;
return [self.dataList[section]title];
}
// ViewController.m
// UITableView数据分离
//
// Created by mac mini on 15-3-12.
// Copyright (c) 2015年 SW. All rights reserved.
//
#import"ViewController.h"
#import "SWStudent.h" // 第二步:添加刚才创建的类
/**
* 第六步 -在storyboard中连线的时候选择的是什么这里写协议的时候就写成什么
*
* 两种协议:
*
* 1. <控件的名字 + DataSource> -定义的与数据有关的方法 - <UITableViewDataSource>
*
* 2. <控件的名字 + Delegate> -定义的与事件有关的方法(滚动,拖拽)通常用来监听控件的事件的 - <UITableViewDelegate>
*/
@interface ViewController () <UITableViewDataSource>
/**
* 第三步 -创建一个 -数据列表
*/
@property (nonatomic,strong)NSArray *dataList;
@end
@implementation ViewController
/**
* 第四步:数据列表 -懒加载
*
* @return 加载好的数据来显示
*/
- (NSArray *)dataList{
if (_dataList == nil) {
SWStudent *stu1 = [[SWStudentalloc]init];
stu1.title =@"2013.3.12";
stu1.desc =@"从新开始学习,第一遍";
//生成编号数组
NSMutableArray *arrayM1 = [NSMutableArrayarray];
for (int i = 0; i <10 ; i++) {
[arrayM1 addObject:[NSStringstringWithFormat:@"%@ - %04d",stu1.title,i]];
}
stu1.students = arrayM1 ;
SWStudent *stu2 = [[SWStudentalloc]init];
stu2.title =@"2013.3.12.20.59";
stu2.desc =@"网开科技";
//生成编号数组
NSMutableArray *arrayM2 = [NSMutableArrayarray];
for (int i = 0 ; i <5 ; i++) {
[arrayM2 addObject:[NSStringstringWithFormat:@"%@ - %03d",stu2.title,i]];
}
stu2.students = arrayM2;
SWStudent *stu3 = [[SWStudentalloc]init];
stu3.title =@"我是三组头";
stu3.desc =@"我是三组尾";
//创建一个生成编号的数组
NSMutableArray *arrayM3 = [NSMutableArrayarray];
for (int i = 0; i <9; i++) {
//把生成的字符串放到arrayM3中(太聪明了)
[arrayM3 addObject:[NSStringstringWithFormat:@"%@ - %02d",stu3.title,i]];
}
stu3.students = arrayM3;
SWStudent *stu4 = [[SWStudentalloc]init];
stu4.title =@"第四组头";
stu4.desc =@"第四组尾";
NSMutableArray *arrayM4 = [NSMutableArrayarray];
for (int i = 0; i <2; i++) {
[arrayM4 addObject:[NSStringstringWithFormat:@"%@ - %01d",stu4.title,i]];
}
stu4.students = arrayM4;
_dataList =@[stu2,stu1,stu3,stu4];
}
return_dataList ;
}
#pragma mark - UITableView的数据源方法<UITableViewDataSource>
// 如果没有实现,默认是1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
//返回数组的数据
returnself.dataList.count;
}
// 每个分组中得数据总数
// sction:分组的编号
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//取出 -数据列表 -中得分组的编号 -自己的理解
//取出数组中对应的学员信息 -教师的注释
//为什么用SWStudent这个自己创建的类来创建一个新的对象,并给这个对象赋值的是这个类中的数据列表的分组编号?
//这是因为:在下边返回每一行有多少行数的时候,需要调用的是“学员编号数组”的数组,而“学员编号数组”是在SWStudent这个自己创建的类里面创建的。
SWStudent *stu =self.dataList[section];
//返回 -对象stu -里面 -学员编号数组 -的个数(就是你在前边用SWStudent这个类中的student数组创建了几个数组,把这几个数组中有几行显示出来)
return stu.students.count;
}
// 告诉表格控件,每一行cell单元格的细节
// indexPath
// @property(nonatomic,readonly) NSInteger section; 分组
// @property(nonatomic,readonly) NSInteger row; 行
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//实例化TableViewCell时,使用initWithStyle方法来进行实例化
UITableViewCell *tableViewCell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:nil];
//取出indexPath对应的数据
// indexPath总共有2个属性,一个是section(分组),一个是row(行数)
//提取出来student对象的“学员编号数组”的分组信息
// SWStudent *perpor -中的perpor对象名称要是改写成其他的名称,那么后面的perpor.students[indexPath.section]和 perpor.students[indexPath.row]的perpor对象名称也要进行改变
SWStudent *perpor = perpor.students[indexPath.section];
//每一个cell中得text都是由以SWStudent为基础创建的对象perpor中得每一行来显示
tableViewCell.textLabel.text = perpor.students[indexPath.row];
return tableViewCell;
}
/**
* 返回每一个分组中的尾部信息
*/
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
// SWStudent *footerTitle = self.dataList[section];
//
// return footerTitle.desc;
//直接总数组中取出的对象是id类型,因为没有明确的类型,因此不能使用点语法(.语法),只能使用getter方法
//这里为什么不能用student数组?是因为用self调用的?
return [self.dataList[section]desc];
}
/**
* 返回每一个分组中得头部信息
*/
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
// SWStudent *headerTitle = self.dataList[section];
// return headerTitle.title;
return [self.dataList[section]title];
}
@end
0 0
- 【UITableView】02 - 数据分离 - 从新 - 学习笔记
- IOS学习笔记-UITableView绑定数据实例
- UITableView学习笔记
- UITableView学习笔记
- UITableView学习笔记
- UITableView学习笔记
- UITableView学习笔记
- UITableView学习笔记
- UITableView学习笔记
- UITableView学习笔记
- UITableView学习笔记
- UITableView学习笔记
- UITableView学习笔记
- UITableView学习笔记
- UITableView学习笔记
- IOS UITableView 学习笔记
- UITableView学习笔记
- UITableView学习笔记
- 指针函数与函数指针
- Android 进阶 教你打造 Android 中的 IOC 框架 【ViewInject】 (上)
- STL源码剖析 - 第5章 关联式容器 - hash_map
- sonar代码质量管理工具
- UITableViewCell 在IOS7的父视图是UITableViewWrapperView。
- 【UITableView】02 - 数据分离 - 从新 - 学习笔记
- byte , bit , kB ,B MB KB GB TB之间的关系
- 碎碎念android eMMC
- JavaWeb: 报错信息The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
- 汕头正全科技网络技术有限公司Android工程师面经
- UVA - 10382 Watering Grass(贪心)
- 1-100中含9个数
- android五种数据存储计数详解
- 最近研究QTP和.net可以结合使用发送邮件,自创方法记录一下