iOS UITableView(二) 为tableView添加数据
来源:互联网 发布:fiddler拦截修改数据 编辑:程序博客网 时间:2024/05/22 09:40
//我们上一章创建了一个tableView我们来添加点数据吧
#import "ViewController.h"
//代理
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
UITableView *_tableView;
//创建数据源数组
NSMutableArray *_dataArry;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
[selfcreatData];
[selfcreatTableView];
}
//我们这里来设计一个二维数组
-(void)creatData{
_dataArry=[[NSMutableArrayalloc]init];
for (int i =0;i < 10; i ++ ) {
//创建一维数组存储每个分区的cell数据
NSMutableArray *arr=[[NSMutableArrayalloc]init];
for (int j =0; j < 10; j++) {
//存储每一行cell的数据,每一行的cell的数据模型
NSString *str =[NSStringstringWithFormat:@"这是第%d组第%d行",i,j];
[arraddObject:str];
}
[_dataArryaddObject:arr];
}
}
-(void)creatTableView{
//下面注释这句我们可以慢慢体会以后会给各位解答
// self.automaticallyAdjustsScrollViewInsets = NO;
//此处的UITableViewStylePlain,UITableViewStyleGrouped大家可以自己试验体会下
_tableView =[[UITableViewalloc]initWithFrame:CGRectMake(0,64, self.view.frame.size.width,self.view.frame.size.height)style:UITableViewStylePlain];
//设置数据源
_tableView.dataSource =self;
//设置代理
_tableView.delegate =self;
[self.viewaddSubview:_tableView];
}
#pragma mark - UITableViewDataSource协议
//设置有多少分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return_dataArry.count;
}
//每个分区有多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [_dataArry[section]count];
}
//获取cell 每次显示cell 之前都要调用这个方法
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//创建复用标识符
static NSString *identifire =@"identifier";
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:identifire];
if (!cell) {//如果没有可以复用的
cell =[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identifire];
}
//填充cell 把数据模型中的存储数据 填充到cell中
cell.textLabel.text=_dataArry[indexPath.section][indexPath.row];
return cell;
}
//设置头标
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [NSStringstringWithFormat:@"这是第%ld组",section];
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//下面来看看效果吧
下面我们来认识一下cell的风格 上面的是第一种
// UITableViewCellStyleDefault,// Simple cell with text label and optional image view (behavior of UITableViewCell in iPhoneOS 2.x)
// UITableViewCellStyleValue1,// Left aligned label on left and right aligned label on right with blue text (Used in Settings)
// UITableViewCellStyleValue2,// Right aligned label on left with blue text and left aligned label on right (Used in Phone/Contacts)
// UITableViewCellStyleSubtitle// Left aligned label on top and left aligned label on bottom with gray text (Used in iPod).
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//创建复用标识符
static NSString *cellID =@"cellID";
//
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {//如果没有可复用的
cell = [[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:cellID] autorelease];
}
//填充cell 把数据模型中的存储数据 填充到cell中
/*
//获取一维数组
NSArray *arr = _dataArr[indexPath.section];
f
*/
UserModel *model = _dataArr[indexPath.section][indexPath.row];
cell.imageView.image = [UIImageimageNamed:model.headName];
cell.textLabel.text = model.userName;
cell.detailTextLabel.text = model.phoneNumber;
return cell;
}
cell =[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleValue1reuseIdentifier:identifire];
cell =[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleValue2reuseIdentifier:identifire];
大家可以自己体会 明天决定多写 写一个类似电话薄的东西添加图片和编辑功能
- iOS UITableView(二) 为tableView添加数据
- iOS UITableView(三) 为tableView添加图片实现电话薄
- ios tableView那些事(二)给tableView添加数据
- ios tableView那些事(二)给tableView添加数据
- iOS UITableView(九) 给tableView添加索引
- ios tableView那些事 (十八) UITableView单行刷新数据
- ios tableView那些事 (十八) UITableView单行刷新数据
- iOS中为tableView的section添加弧形
- iOS开发~为UITableView的reloadData方法添加动画
- iOS UITableView(四) 为tableView设置头尾视图,改变cell的宽度,cell的缩进
- 【iOS代码】Json数据展示为联系人效果的TableView
- IOS TableView详解(二)
- iOS UITableView(一)tableView的创建
- iOS UITableView(十一) tableView的下拉刷新
- iOS 竖向tableView上添加横向tableView
- IOS tableView数据显示
- iOS开发之UITableView数据为空的提示页面
- ios tableview 那些事(二十五)用故事板 自定义Tableview 不显示数据注意事项。
- C++对象 内存分布
- ez_SQL 单例
- iOS应用崩溃(三)——崩溃日志
- MYECLIPSE 2014 有用的几个快捷键
- Linux有问必答:如何在树莓派上安装USB网络摄像头
- iOS UITableView(二) 为tableView添加数据
- MyBatis Generator generatorConfig.xml配置详解
- C++.OOP
- 手淘双十一系列(一) | 521 性能优化项目揭秘
- PDF文档合并器
- 返回值(null)和<null>处理【空指针和野指针】
- 1067. Sort with Swap(0,*) (25)【贪心】——PAT (Advanced Level) Practise
- java(一)编程工具的下载与环境变量的配置
- Ubuntu 安装Flask