UITabView

来源:互联网 发布:织梦5.7自动内链失效 编辑:程序博客网 时间:2024/04/30 18:55
表视图tabView
tabView继承自UIScrollView : UIView : UIResponder : NSObject

初始化方法
- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style; 
例子:
UITableView * tableView=[[UITableViewalloc]initWithFrame:CGRectMake(0, -50,320,466)style:UITableViewStylePlain];

 tabViewStyle样式

UITableViewStylePlain,                  

UITableViewStyleGrouped


UITabView属性

Style  separatorColor   tableHeaderView   tableFooterView等

UITabView两个重要的代理方法
UITabViewDataSource 和UITabViewDelegate
 tabView的代理dataSource中必须实现的两个方法:

//返回每个分区的行数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; 

//返回cell也就是行

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; 



dataSource中可实现也可不实现的方法//返回多少格分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
//设置每个分区的头和脚
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;    
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
单元格重用机制

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

NSLog(@"返回单元格");

//定义一个重用标识符

staticNSString * reuseIndentiffer=@"cell";

//可以是任意唯一的字符串//从重用队列里面按某个标示符拿可以重用的单元格

UITableViewCell * cell=[tableViewdequeueReusableCellWithIdentifier:reuseIndentiffer];

//如果重用队列里面没有可用的单元格,创建单元格

if (cell==nil

    {

  cell=[[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:reuseIndentiffer]autorelease];   

     }

return cell;//返回创建好的单元格

} 


UITabViewCell初始化方法
  - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier;
用在-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法中

UITabViewCell系统自定义的四种样式

UITableViewCellStyleDefault;  

UITableViewCellStyleValue1;  

UITableViewCellStyleValue2,;

UITableViewCellStyleSubtitle;



UITabViewCell属性多多要非常注意了
主要有 :
     backgroundView
     contentView
     imageView
     accessoryView
     textLabel
     detailTextLabel

//选中单元格执行的代理方法 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

DetailViewController * detailView=[[DetailViewControlleralloc]init]; //关联新窗口

[self.navigationControllerpushViewController:detailViewanimated:YES]; //点击单元格推出一个新界面

[detailView release];}



//点击cell上详细视图按钮执行的代理方法

-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath

{   DetailViewController * detailView=[[DetailViewControlleralloc]init];   

[self.navigationControllerpushViewController:detailViewanimated:YES];

   [detailView release];}