IOS 学习笔记(9)tableView基础
2013-07-18 11:23
447 查看
TableView是一个被分成不同部分的滚动视图,每一部分又进一步被分成行,每行是一个UITableViewCell类的实例。可以把图片,文本和其他任何东西嵌入tableView单元格,可以自定义他们的形状,高度,分组或更多。这些分别在UITableViewDataSource和UITableViewDelegate的协议来定义。
#import<UIKit/UIKit.h>
@interface TableViewController:UIViewController
@property(monatomic,strong)UITableView *myTableView;
@end
@implementation TableViewController
@synthesize myTableView;
-(void)viewDidLoad{
[super viewDidLoad];
self.myTableView = [ [UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
[self.view addSubview:self.myTableView];
}
这样就创建了一个空白的TableView
UITableViewStylePlain
创建一个没有背景图片的空白 Table View
UITableViewStyleGrouped
创建一个有背景图片和圆角组边框的 Table View,类似于 Settings app。
怎么添加数据呢?
遵循UITableViewDelegate协议:
//设置tableviewCell高度
-(void)viewDidLoad{
[super viewDidLoad];
CGRect tableViewFrame = self.view.bounds;
self.myTableView = [[UITableView alloc]initWithFrame:tableViewFrame style:UITableViewStylePlain];
self.myTableView.delegate = self;
[self.view addSubview:self.myTableView];
}
-(void)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
CGFloat result = 20.0f;
if([tableView isEqual:self.myTableView]){
result = 40.0f;
}
return result;
}
TableView中的单元格的位置由其索引路径展示出来,一个索引路径是section和row索引的组合。section索引是从零开始的。
添加数据:
添加数据还需要遵循UITableViewDataSource协议
添加一下代码
self.myTableView.dataSource = self;
self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
NSInteger result = 0;
if([tableView isEqual:self.myTableView]){
result = 3;
}
return result;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSInteger result = 0;
if([tableView isEqual:self.myTableView]){
switch(section){
case 0:{
result = 3;
break;
}
case 1:{
result = 5;
break;
}
case 2:{
result = 8;
break;
}
}
}
return result;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *result = nil;
if([tableView isEqual:self.myTableView]){
static NSString *TableViewCellIdentifier = @"MyCells";
result = [tableView dequeueReusableCellWithIdentifier:TableViewCellIdentifier];
if(result == nil){
result = [ [UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableViewCellIdentifier];
}
result.textLabel.text = [NSString stringWithFormat:@"Section %ld,Cell %ld",(long)indexPath.section,(long)indexPath.row];
}
return result;
}
接收和处理Table view事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if([tableView isEqual:self.myTableView]){
NSLog(@"%@",[NSString stringWithFormat:@"Cell %ld in Section %ld is Selected",(long)indexPath.row,(long)indexPath.section]);
}
}
在TableView中使用不同种类的附件
result.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
创建自定义tableView单元格附件
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *result = nil;
static NSString *myCellIdentifier = @"SimpleCell";
result = [tableView dequeueReusableCellWithIdentifier:myCellIdentifier];
if(result == nil){
result = [ [UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myCellIdentifier];
}
result.textLabel.text = [NSString stringWithFormat:@"Section %ld,Cell %ld",(long)indexPath.section,(long)indexPath.row];
UIButton *button = [UIButton buttonWithType:UIButtonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(0.0f,0.0f,150.0f,25.0f);
[button setTitle:@"Expand" forState:UIControlStateNormal];
[button addTarget:self action:@selector(performExpand:) forControlEvents:UIControlEventTouchUpInside];
result.accessoryView = button;
return result;
}
-(void)performExpand:(id)paramSender{
/*Take an at ion here*/
UITableViewCell *ownerCell = (UITableViewCell *)paramSender:superview;
if(ownerCell != nil){
NSIndexPath *ownerCellIndexPath = [self.myTableView indexPathForCell:ownerCell];
NSLog(@"Accessory in index path is tapped. indexPath = %@",ownerCellIndexPath);
if(ownerCellIndexPath.section == 0 && ownerCellIndexPath.row == 1){
/*This is the second row in the first section*/
}
}
}
#import<UIKit/UIKit.h>
@interface TableViewController:UIViewController
@property(monatomic,strong)UITableView *myTableView;
@end
@implementation TableViewController
@synthesize myTableView;
-(void)viewDidLoad{
[super viewDidLoad];
self.myTableView = [ [UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
[self.view addSubview:self.myTableView];
}
这样就创建了一个空白的TableView
UITableViewStylePlain
创建一个没有背景图片的空白 Table View
UITableViewStyleGrouped
创建一个有背景图片和圆角组边框的 Table View,类似于 Settings app。
怎么添加数据呢?
遵循UITableViewDelegate协议:
//设置tableviewCell高度
-(void)viewDidLoad{
[super viewDidLoad];
CGRect tableViewFrame = self.view.bounds;
self.myTableView = [[UITableView alloc]initWithFrame:tableViewFrame style:UITableViewStylePlain];
self.myTableView.delegate = self;
[self.view addSubview:self.myTableView];
}
-(void)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
CGFloat result = 20.0f;
if([tableView isEqual:self.myTableView]){
result = 40.0f;
}
return result;
}
TableView中的单元格的位置由其索引路径展示出来,一个索引路径是section和row索引的组合。section索引是从零开始的。
添加数据:
添加数据还需要遵循UITableViewDataSource协议
添加一下代码
self.myTableView.dataSource = self;
self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
NSInteger result = 0;
if([tableView isEqual:self.myTableView]){
result = 3;
}
return result;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSInteger result = 0;
if([tableView isEqual:self.myTableView]){
switch(section){
case 0:{
result = 3;
break;
}
case 1:{
result = 5;
break;
}
case 2:{
result = 8;
break;
}
}
}
return result;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *result = nil;
if([tableView isEqual:self.myTableView]){
static NSString *TableViewCellIdentifier = @"MyCells";
result = [tableView dequeueReusableCellWithIdentifier:TableViewCellIdentifier];
if(result == nil){
result = [ [UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableViewCellIdentifier];
}
result.textLabel.text = [NSString stringWithFormat:@"Section %ld,Cell %ld",(long)indexPath.section,(long)indexPath.row];
}
return result;
}
接收和处理Table view事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if([tableView isEqual:self.myTableView]){
NSLog(@"%@",[NSString stringWithFormat:@"Cell %ld in Section %ld is Selected",(long)indexPath.row,(long)indexPath.section]);
}
}
在TableView中使用不同种类的附件
result.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
创建自定义tableView单元格附件
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *result = nil;
static NSString *myCellIdentifier = @"SimpleCell";
result = [tableView dequeueReusableCellWithIdentifier:myCellIdentifier];
if(result == nil){
result = [ [UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myCellIdentifier];
}
result.textLabel.text = [NSString stringWithFormat:@"Section %ld,Cell %ld",(long)indexPath.section,(long)indexPath.row];
UIButton *button = [UIButton buttonWithType:UIButtonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(0.0f,0.0f,150.0f,25.0f);
[button setTitle:@"Expand" forState:UIControlStateNormal];
[button addTarget:self action:@selector(performExpand:) forControlEvents:UIControlEventTouchUpInside];
result.accessoryView = button;
return result;
}
-(void)performExpand:(id)paramSender{
/*Take an at ion here*/
UITableViewCell *ownerCell = (UITableViewCell *)paramSender:superview;
if(ownerCell != nil){
NSIndexPath *ownerCellIndexPath = [self.myTableView indexPathForCell:ownerCell];
NSLog(@"Accessory in index path is tapped. indexPath = %@",ownerCellIndexPath);
if(ownerCellIndexPath.section == 0 && ownerCellIndexPath.row == 1){
/*This is the second row in the first section*/
}
}
}
相关文章推荐
- IOS 学习笔记(9)tableView基础
- IOS tableview 学习笔记
- iOS学习笔记---数据库SQLite3的基本操作并讲内容显示在tableView中
- iOS学习笔记之判断tableview向上还是向下滑动!
- ((ios开发学习笔记 十一))自定义TableViewCell 的方式实现自定义TableView(带源码)
- IOS开发学习笔记(四)——使用TableView
- iOS 学习笔记 五 (2015.03.17)使用storyBoard进行tableview的跳转
- IOS学习笔记 -- scrollView和tableView整理
- iOS学习笔记--tableView中如何获取cell上textfiled的值
- iOS学习笔记-TableView性能优化篇1
- ios学习笔记: TableView利用label调整cell高度
- IOS 学习笔记 —— tableView 使用详解(二)
- iOS学习笔记-053.自定义View01——基础
- 2013斯坦福大学iOS应用开发学习笔记 11 Table View and iPad
- iOS学习笔记-- tableView单选的实现
- 第二章、小控件实例 tableview,button(iOS学习笔记,从零开始。)
- iOS学习笔记2—关于tableView的一些简单操作
- ((ios开发学习笔记九)) Simple TableView 实现(附 实例源码)
- IOS学习笔记之八:tableView的创建
- iOS学习笔记-TableView性能优化篇1