浅析IOS中的table view

By | 2014/07/05

浅析IOS中的table view

概述

table view是UITableView的实例(UITableView是继承自UIScrollView但是UITableView只允许垂直的滚动。),我们可以使用tableView中的方法改变tableView的界面,还有一些方法使我们方便的控制tableView行为。在UITableView中有两个重要的属性分别是delegate和data source。data source对象是用来提供填充table view所需要的数据。delegate对象则提供了自定义table view外观的方法。data source遵循UITableViewDataSource协议,delegate遵循UITableViewDelegate协议。date source与delegate往往是一个对象,该对象通常为UIViewController的对象或其子类UITableViewController的对象。注意当界面中有多个view而且其中还有一个table view的话控制器不能是UIViewTableView。因为UITableView会默认将table view全屏显示。

带有导航条的table view

首先来了解一下UINavigtionController,其最基本的功能是根据用户的动作将view controller入栈或出栈。

UINavigationController的操作步骤:

1.初始化:UINavigationController在初始化时可以通过initWithRootViewController方法传入view controller作为跟控制器,这个根控制器永远处于栈的底层(栈中只有一个对象时,其也是栈顶)。 2.设置UIWindow的rootViewController为UINaviationController。 3.根据具体情况通过push方法添加控制器。

UINavigationController的viewControllers栈

@property(nonatomic,copy) NSArray *viewControllers; 使用push方法将view controller压站: – (void)pushViewController:(UIViewController *)viewController animated; 使用pop方法可以移除控制器: 1.移除栈顶控制器 – (UIViewController *)popViewControllerAnimated:(BOOL)animated; 2.回到指定的控制器 -(NSArray  *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated; 3.回到根控制器 – (NSArray *)popToRootViewController Animated:(BOOL)animated;

创建table view的基本原理

1.创建在view controller中创建UITableView实例并且确定其风格。 2.将view controller设置为delegate和datasource的值,接着发送reloadData消息。datasource对象必须遵循UITableViewDataSource协议delegate对象必须遵循UITableDelegate协议。 3.datasource 接收到从table view 发来的numberOfSectionInTableView:消息后发挥table view中section的数目。这个方法不是必须实现的,但是如果tableView有多个section的话就必须实现。 4.对于每一个section,datasource会收到tableView:numberOfRowsInSection:消息,然后datasource会相应section中row的数量。 5.data source收到tableView:cellForRowAtIndexPath:消息,作为响应datasource会返回一个UITableViewCell对象(table view每一行都是一个UITableViewCell对象),然后UITableView对象使用这个cell来绘制每一行。

table_view_setup

 

使用代码创建Table View

1.遵循datasource和delegate协议

[code lang=”objc”]
@inertface RootViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic , strong)NSArray *timeZoneNames;

@end

[/code]

2.创建和配置table view

[code lang=”objc”]
– (void)loadView{
UITableView *tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain];

tableView.delegate = self;

tableView.dataSource = self;

[tableView reloadData];

self.view = tableView;
}
[/code]

3.为动态table view填充数据

当table view创建完毕后,table view对象就收到reloadData消息,接着table view从data source中查找它需要的数据,根据delegate中的方法来生成自己的外观。table view得到了section的数目和每个section中row的数目,则不断的调用方法tableView:cellForRowAtIndexPath:来获得每一个可见行的cell(UITableViewCell);table view使用获得的UITableViewCell对象来绘制row。

[code lang=”objc”]

– (NSInteger)numberOfSectionInTableView:(UITableView *)tableView{
return [regions count];
}

– (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
Region *region = [regions objectAtIndex:section];
return [region.timeZoneWrappers count];
}

– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *MyIdentifier = @"MyReuseIdentifier";
UITableViewCell *cell = [tableview dequeueReusableCellWithIndeifier:MyIdentifier];
if(cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Myidentifier];
Region *region = [regions objectAtIndex:indexPath.section];
cell.textLabel.text = @"temp name";
return cell;
}
}

[/code]

 

 

发表评论

邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据