UItableview详解

2016-03-17  本文已影响126人  流水点点

UItableview 详解

详细介绍

* UITableView及其两种风格三部分

* UITableViewController

* UITableViewCell及其四种风格

*通过代理给UITableView设置cell

*性能优化

*通过代理增加删除cell



UITableView有两种风格:

*1、UITableViewStylePlain普通风格

*2、UITableViewStyleGrouped分组风格

三部分:

1.给tableView设置表头

tableView.tableHeaderView= [selfaddHeaderView];

//挂上代理

tableView.delegate=self;

tableView.dataSource=self;

2.给tableView设置表尾

tableView.tableFooterView= [selfaddFooterView];

UITableViewController就是一个自带UITableView的控制器

3.设置表头的方法

- (UIView*)addHeaderView{

UILabel*label = [[UILabelalloc]initWithFrame:CGRectMake(0,0,0,40)];

label.text=@"表头";

return label;

}

4.设置表尾的方法

- (UIView*)addFooterView{

UILabel*label = [[UILabelalloc]initWithFrame:CGRectMake(0,0,0,40)];

label.text=@"表尾";

return label;

}

- (void)addTableViewCell{

//右边出现小箭头

tabViewCell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;

//圈i加箭头

tabViewCell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;

//对号

tabViewCell.accessoryType=UITableViewCellAccessoryCheckmark;

//圈i

tabViewCell.accessoryType=UITableViewCellAccessoryDetailButton;

}

#pragma mark -通过代理给UITableView-设置cell

两种崩溃调试的方法

1、通过栈台找reason;

2、设置全局断点定位导致崩溃的代码

//返回tabview有多少行

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

{

return8;

}

- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView

{

return3;

}

//指定cell每次在屏幕上显示一个cell该方法就会调用一次

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

{

//UITableViewCell重用机制

// UITableView每次滑动,必定有消失的cell,系统会自动将这些消失的cell放到缓存池里,需要新cell时,系统先在缓存池里看是否有cell,有的话就利用,没有的话就新建。

/*前提:UITableView滑动

1、久的cell消失系统自动将这个cell放到缓存池里面

2、新的cell要显示,就会调用代理的方法。

1)首先看缓存池里面有么有cell

2)如果有cell就利用话没有的话就新建

3)在代理方法中返回设置的cell

*/

staticNSString*cellId =@"cellID";

//UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellId"];

UITableViewCell*cell = [tableViewdequeueReusableCellWithIdentifier:cellId];

if(cell==nil) {

cell =[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:cellId];

}

//cell.textLabel.text = @"二货";

//cell.textLabel.textColor = [UIColor lightGrayColor];

//cell.detailTextLabel.text = @"二货就是二货";

//UIImage *image = [UIImage imageNamed:@"20.tiff"];

//cell.imageView.image = image;

//static NSString *cellId = @"cellID";

//UITableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:@""];

returncell;}

- (CGFloat)tabview:(UITableView*)tableView heightForRowAtindexPath:(NSInteger*)indexPath

{return100;}

#pragma mark---必须实现的两个代理方法-------------

//一组有多少行

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{}

//每行中的cell的实现

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

上一篇下一篇

猜你喜欢

热点阅读