UITableView

转自:UITableView

ios中UITableView,也就是表视图,这在ios系统中是非常常见的,开发这种表视图也是非常方便的,在工具栏中,直接将UITableView拖拉到xib中即可,如下:

uitable1 UITableView

UITableView

在UIViewController之后添加<UITableViewDelegate, UITableViewDataSource>,UITableViewDelegate和 UITableViewDataSource在Objective-C中称之为协议,要实现协议中必要的方法(因为有可选的方法)。为什么要使用这两个协议呢?因为我们要将数据填充到UITableView中,这样子,那UITableViewDelegate和 UITableViewDataSource应该与数据填充有关了,其实看它的命名也可以看得出了。

UITableViewDataSource是用来连接数据和表视图的,要实现两个方法,一个是tableView:cellForRowAtIndexPath,另一个是tableView:numberOfRowsInSection,实现这两个方法,你就告诉了表视图显示多少行数据和每一行中的数据。

UITableViewDelegate是负责处理UITableView的表现,该协议中的可选方法让你管理表行的高度,配置节点头部和底部,对表单元重新排序等。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#import "SimpleTableViewController.h"
 
@implementation SimpleTableViewController
 
NSArray *tableData;
 
- ( 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.
}
 
#pragma mark - View lifecycle
 
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
*/
 
- ( void )viewDidUnload
{
[ super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
 
- ( BOOL )shouldAutorotateToInterfaceOrientation:( UIInterfaceOrientation )interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation ==  UIInterfaceOrientationPortrait );
}
 
@end

viewDidLoad是在控制器的视图装载到内存中完成之后,调用该方法,添加以下代码实例化tableData数组:

1
2
3
4
5
6
7
8
9
10
- ( void )viewDidLoad
{
     [ super viewDidLoad];
     tableData=[ NSArray arrayWithObjects: @"Egg Benedict" ,
                @"Mushroom Risotto" , @"Full Breakfast" ,
                @"Hamburger" , @"Ham and Egg Sandwich" ,
                @"Creme Brelee" , @"White Chocolate" ,
                @"Starbucks Coffee" , @"Vegetable Curry" ,
                @"Instant Noodle with Egg" , nil ];
}

实现UITableViewDataSource协议中的两个方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//count
-( NSInteger )tableView:( UITableView *)tableView numberOfRowsInSection:( NSInteger )section
{
     return [tableData count];
}
 
-( UITableViewCell *)tableView:( UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath *)indexPath
{
     static NSString *simpleTableIdentifier= @"SimpleTableItem" ;
     UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
     if (cell== nil ){
         cell=[[ UITableViewCell alloc]initWithStyle: UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
     }
     cell.textLabel.text=[tableData objectAtIndex:indexPath.row];
     return cell;
}

count方法返回tableData数组中元素个数,每行数据显示的时候,会调用cellForeRowAtIndexPath 方法。dequeueReusableCellWithIdentifier: 方法返回的是一个可重用的表规图单元格对象。因为如果表非常大,为每一行都创建一个单独的 UITableViewCell对象会产生严重的性能问题,会占用大量的内存。 此外,由于表视图在某一个时刻会显示固定数量的行,因此重用已经滚动到屏幕外面的那些单元格将非常有意义。 这正是dequeueReusableCellWithIdentifier: 方法将要完成的事情。比如,如果表视图显示了 10 行,那么会创建 10 个 UITableViewCell 对象 — 当用户滚动表视图时,总是会重用返 10 个 UITableViewCell 对象。

SimpleTableViewController.xib 文件,点击并按住 Control 键,选择表视图,并拖拉到 Files Owner图上,释放按钮,弹出 dataSource 和 delegate 窗口。选择 dataSource,在表视图和它的数据源之间建立连接。重复上述操作,在委托(delegate)上也建立连接。

运行如下:

iphone UITableView

转载于:https://www.cnblogs.com/hxxy2003/archive/2013/04/16/3023673.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值