Iphone UITableView 创建最简单的表格
2011-04-11 10:20
274 查看
利用UITableView创建最简单的表视图。就是每一列中显示一行文字。效果图如下:
表视图并不负责存储数据,只是负责展示相关的内容,表视图中遵循UITableViewDelegate和
UITableViewDataSource协议。
首先创建一个视图项目:
声明代码:
#import
<UIKit/UIKit.h>
@interface
iphone_tableviewViewController : UIViewController
<UITableViewDelegate,UITableViewDataSource>{
NSArray
*
listData;
}
@property (
nonatomic,
retain) NSArray *listData;
@end
实现代码:
#import
“iphone_tableviewViewController.h”
@implementation
iphone_tableviewViewController
@synthesize listData;
- (void
)viewDidLoad {
NSArray
*array= [[
NSArray
alloc
]
initWithObjects
:
@”你好
“,
@”你好
“,
@”你好
“,
@”你好
“,
@”你好
“,
@”你好
“,
@”你好
“,
@”你好
“,
@”你好
“,
@”你好
“,
@”你好
“,
nil
];
self
.listData
=array;
[array release
];
[
super
viewDidLoad];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void
)didReceiveMemoryWarning {
// Releases the view if it doesn’t have a superview.
[
super
didReceiveMemoryWarning];
// Release any cached data, images, etc that aren’t in use.
}
- (void
)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void
)dealloc {
[
listData release
];
[super
dealloc
];
}
//
添加行数
-(NSInteger
) tableView:(UITableView
*)tableView
numberOfRowsInSection:(NSInteger
)section
{
return [
self.
listData
count
];
}
//
添加每一行的信息
- (UITableViewCell
*) tableView:(UITableView
*)tableView
cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
static
NSString
*tag=@”tag”
;
UITableViewCell
*cell=[tableView
dequeueReusableCellWithIdentifier:tag];
if
(cell==nil
) {
cell=[[[
UITableViewCell alloc
]
initWithFrame
:
CGRectZero
reuseIdentifier:tag]
autorelease];
}
NSUInteger
row=[indexPath row
];
cell.text
=[listData
objectAtIndex
:row];
return
cell;
}
@end
最后打开Interface Builder,添加相应的视图,并且相关联:
编译运行既可。
官方文档:
http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITableView_Class/Reference/Reference.html#//apple_ref/occ/instm/UITableView/cellForRowAtIndexPath:
源代码:
http://easymorse.googlecode.com/svn/trunk/iphone.tableview/
表视图并不负责存储数据,只是负责展示相关的内容,表视图中遵循UITableViewDelegate和
UITableViewDataSource协议。
首先创建一个视图项目:
声明代码:
#import
<UIKit/UIKit.h>
@interface
iphone_tableviewViewController : UIViewController
<UITableViewDelegate,UITableViewDataSource>{
NSArray
*
listData;
}
@property (
nonatomic,
retain) NSArray *listData;
@end
实现代码:
#import
“iphone_tableviewViewController.h”
@implementation
iphone_tableviewViewController
@synthesize listData;
- (void
)viewDidLoad {
NSArray
*array= [[
NSArray
alloc
]
initWithObjects
:
@”你好
“,
@”你好
“,
@”你好
“,
@”你好
“,
@”你好
“,
@”你好
“,
@”你好
“,
@”你好
“,
@”你好
“,
@”你好
“,
@”你好
“,
nil
];
self
.listData
=array;
[array release
];
[
super
viewDidLoad];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void
)didReceiveMemoryWarning {
// Releases the view if it doesn’t have a superview.
[
super
didReceiveMemoryWarning];
// Release any cached data, images, etc that aren’t in use.
}
- (void
)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void
)dealloc {
[
listData release
];
[super
dealloc
];
}
//
添加行数
-(NSInteger
) tableView:(UITableView
*)tableView
numberOfRowsInSection:(NSInteger
)section
{
return [
self.
listData
count
];
}
//
添加每一行的信息
- (UITableViewCell
*) tableView:(UITableView
*)tableView
cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
static
NSString
*tag=@”tag”
;
UITableViewCell
*cell=[tableView
dequeueReusableCellWithIdentifier:tag];
if
(cell==nil
) {
cell=[[[
UITableViewCell alloc
]
initWithFrame
:
CGRectZero
reuseIdentifier:tag]
autorelease];
}
NSUInteger
row=[indexPath row
];
cell.text
=[listData
objectAtIndex
:row];
return
cell;
}
@end
最后打开Interface Builder,添加相应的视图,并且相关联:
编译运行既可。
官方文档:
http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITableView_Class/Reference/Reference.html#//apple_ref/occ/instm/UITableView/cellForRowAtIndexPath:
源代码:
http://easymorse.googlecode.com/svn/trunk/iphone.tableview/
相关文章推荐
- 使用 UITableView 创建表格应用演练(1)——一个简单的表格应用
- 使用 UITableView 创建表格应用演练(1)——一个简单的表格应用
- 使用 UITableView 创建表格应用演练(4)——自定义单元格
- iOS中表格(UITableView)嵌套表格的简单实现
- 使用 UITableView 创建表格应用演练(2)——从plist文件加载并显示数据
- swift之表格控件简单使用(UITableview)
- swift:创建表格UITableView
- UItableView和UISearchBar实现简单表格搜索
- iPhone开发入门3 - 创建一个UITable View程序
- 创建一个简单的表视图&自定义UITableView的表单元格
- UItableView和UISearchBar实现简单表格搜索
- iphone开发之表格组件UITableView的使用(五)UITableView与UItableViewCell常见属性以及单元格重用
- iphone开发之表格组件UITableView的使用(七)实现数据刷新
- iPhone开发入门4 - 创建一个UITable View程序,不使用Interface Builder
- iphone开发之表格组件UITableView的使用(一)使用时的具体步骤介绍
- UITableView 使用方法(一) - 创建简单的列表
- iphone开发之表格组件UITableView的使用(六)通过加载plist文件展示汽车品牌并添加索引查找
- UITableView 使用方法(一) - 创建简单的列表
- UIXX-UITableView制作简单表格
- Xcode9学习笔记40 - 使用UITableView制作简单表格