ios tableView那些事(二)给tableView添加数据
2013-08-28 14:19
483 查看
我们上一章已经创建了tableview现在我们给它添加点数据吧
#import <UIKit/UIKit.h>
/*tableview 一定要用到这两个delegate UITableViewDataSource,UITableViewDelegate */
@interface ViewController :UIViewController <UITableViewDataSource,UITableViewDelegate>
{
UITableView *tableview;
NSArray *array; //创建个数组来放我们的数据
}
@property (strong,nonatomic)UITableView *tableview;
@property (strong,nonatomic)NSArray *array;
@end
#import "ViewController.h"
@interfaceViewController ()
@end
@implementation ViewController
@synthesize tableview;
@synthesize array;
- (void)viewDidLoad
{
[superviewDidLoad];
tableview = [[UITableViewalloc]initWithFrame:CGRectMake(0, 0,self.view.bounds.size.width,self.view.bounds.size.height)style:UITableViewStylePlain];
// UITableViewStylePlain,
// UITableViewStyleGrouped
tableview.delegate =self;//不要忘写了这两句话哟调用delegate*/
tableview.dataSource=self;
[self.viewaddSubview:tableview];
NSMutableArray *arrayValue = [[NSMutableArrayalloc]init];
for (int i = 0; i< 10; i++)
{
NSString *value = [NSStringstringWithFormat:@"%d",i];
[arrayValue addObject:value];
}
array = arrayValue;
}
/* 这个函数是显示tableview的章节数*/
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
return 1;
}
/* 这个函数是指定显示多少cells*/
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [arraycount];//这个是指定加载数据的多少即显示多少个cell,如果这个地方弄错了会崩溃的哟
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//定义个静态字符串为了防止与其他类的tableivew重复
static
NSString *CellIdentifier = @"Cell";
//定义cell的复用性当处理大量数据时减少内存开销
UITableViewCell *cell = [tableviewdequeueReusableCellWithIdentifier:CellIdentifier];
if (cell ==nil)
{
cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyle: UITableViewCellStyleDefault:CellIdentifier];
}
cell.textLabel.text = [arrayobjectAtIndex:[indexPathrow]]; //通过 [indexPath row]
遍历数组
return cell;
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
是不是很简单
![](http://img.my.csdn.net/uploads/201308/28/1377667194_6612.png)
现在有些过于简单那么我们在加些东西吧
我们的cell styl 也有四种样式
cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];
上图是默认格式
// 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
{
//定义个静态字符串 为了防止与其他类的tableivew重复
static NSString *CellIdentifier = @"Cell";
//定义cell 的复用性 当处理大量数据时减少内存开销
UITableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [array objectAtIndex:[indexPath row]]; //通过 [indexPath row]
遍历数组
cell.detailTextLabel.text
= [arrayobjectAtIndex:[indexPathrow]];
return cell;
}
![](http://img.my.csdn.net/uploads/201308/28/1377667903_5296.png)
是不是有种熟悉的感觉呢
接下来看看剩下的两种风格
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1reuseIdentifier:CellIdentifier];
![](http://img.my.csdn.net/uploads/201308/28/1377670547_8917.png)
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2reuseIdentifier:CellIdentifier];
#import <UIKit/UIKit.h>
/*tableview 一定要用到这两个delegate UITableViewDataSource,UITableViewDelegate */
@interface ViewController :UIViewController <UITableViewDataSource,UITableViewDelegate>
{
UITableView *tableview;
NSArray *array; //创建个数组来放我们的数据
}
@property (strong,nonatomic)UITableView *tableview;
@property (strong,nonatomic)NSArray *array;
@end
#import "ViewController.h"
@interfaceViewController ()
@end
@implementation ViewController
@synthesize tableview;
@synthesize array;
- (void)viewDidLoad
{
[superviewDidLoad];
tableview = [[UITableViewalloc]initWithFrame:CGRectMake(0, 0,self.view.bounds.size.width,self.view.bounds.size.height)style:UITableViewStylePlain];
// UITableViewStylePlain,
// UITableViewStyleGrouped
tableview.delegate =self;//不要忘写了这两句话哟调用delegate*/
tableview.dataSource=self;
[self.viewaddSubview:tableview];
NSMutableArray *arrayValue = [[NSMutableArrayalloc]init];
for (int i = 0; i< 10; i++)
{
NSString *value = [NSStringstringWithFormat:@"%d",i];
[arrayValue addObject:value];
}
array = arrayValue;
}
/* 这个函数是显示tableview的章节数*/
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
return 1;
}
/* 这个函数是指定显示多少cells*/
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [arraycount];//这个是指定加载数据的多少即显示多少个cell,如果这个地方弄错了会崩溃的哟
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//定义个静态字符串为了防止与其他类的tableivew重复
static
NSString *CellIdentifier = @"Cell";
//定义cell的复用性当处理大量数据时减少内存开销
UITableViewCell *cell = [tableviewdequeueReusableCellWithIdentifier:CellIdentifier];
if (cell ==nil)
{
cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyle: UITableViewCellStyleDefault:CellIdentifier];
}
cell.textLabel.text = [arrayobjectAtIndex:[indexPathrow]]; //通过 [indexPath row]
遍历数组
return cell;
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
是不是很简单
![](http://img.my.csdn.net/uploads/201308/28/1377667194_6612.png)
现在有些过于简单那么我们在加些东西吧
我们的cell styl 也有四种样式
cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];
上图是默认格式
// 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
{
//定义个静态字符串 为了防止与其他类的tableivew重复
static NSString *CellIdentifier = @"Cell";
//定义cell 的复用性 当处理大量数据时减少内存开销
UITableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [array objectAtIndex:[indexPath row]]; //通过 [indexPath row]
遍历数组
cell.detailTextLabel.text
= [arrayobjectAtIndex:[indexPathrow]];
return cell;
}
![](http://img.my.csdn.net/uploads/201308/28/1377667903_5296.png)
是不是有种熟悉的感觉呢
接下来看看剩下的两种风格
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1reuseIdentifier:CellIdentifier];
![](http://img.my.csdn.net/uploads/201308/28/1377670547_8917.png)
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2reuseIdentifier:CellIdentifier];
![](http://img.my.csdn.net/uploads/201308/28/1377670557_6895.png)
相关文章推荐
- ios tableView那些事(二)给tableView添加数据
- iOS UITableView(二) 为tableView添加数据
- 【iOS开发-60】案例学习:多组数据的tableView设置、添加右側组索引、多层数据模型设置以及valueForKeyPath
- iOS tableView 数据处理,数据分类相同数据整合、合并计算总数总价
- iOS 为tableview添加新的cell类
- iOS的tableview与请求刷新数据一块的话.
- iOS 刷新tableView的某一行的row的时候,row的高度增加,数据也增加。
- ios tableView那些事 (十八) UITableView单行刷新数据
- IOS - TableView上拉刷新数据后设置显示偏移(新数据置顶)
- ios tableView那些事 (七) 给tableView添加响应事件
- iOS用SegmentController来切换tableView的时候 reloadData会造成数据重复创建 解决办法
- IOS中tableView的数据优化
- IOS开发中tableView显示列表内容数据(storyboard版)
- ios tableView那些事(四)tableView添加标题,头尾和改变cell的宽度 分类: Ios tableview 2013-08-28 19:07 12096人阅读 评论(0) 收藏
- ios中要在tableview中添加事件的方法
- IOS TableView 数据列表与设置页面(个人)
- iOS学习- 12 绑定数组数据到Table View - Todo App
- 当数据量很少的时候,tableview会显示多余的cell--iOS开发系列---项目中成长的知识二
- ios tableView那些事(三)给tableView添加些图片
- iOS 在tableView上添加button导致按钮没有点击效果和不能滑动的 zhuang