美文网首页
UI总结-tableView的基本用法

UI总结-tableView的基本用法

作者: Dear丶Musk | 来源:发表于2016-05-20 20:38 被阅读1105次

        UI总结-tableView的基本用法

     对于UITableView,我們有一些特殊的概念和术语,比如说我们成Table View的一行为Cell,而许多的Cell可以组成Section,每个Section上下又分別有Header和Footer,许多个的Section则组成了整个Table ,当然Table也有Header和Footer.现在市面上的APP的大多数界面都是由tableView铺成的,以后会经常用到.

ViewController.m文件:

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic, retain)UITableView *tableView;

@end

@implementation ViewController

- (void)viewDidLoad {    

[super viewDidLoad];    

// Do any additional setup after loading the view, typically from a nib.    self.view.backgroundColor = [UIColor whiteColor];    

self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64)];    

[self.view addSubview:self.tableView];    

[_tableView release];    

//设置tableView的行高    

self.tableView.rowHeight = 100;   

 //设置代理人    

self.tableView.dataSource = self;    

self.tableView.delegate = self;    

//下面这个属性是ViewController的属性,作用于 第一个滚动试图,作用就是让滚动试图自动去适应尺寸    

self.automaticallyAdjustsScrollViewInsets = NO;    

}

#pragma mark-第一个必须实现的协议方法,用来设置每个分区有多少行.

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

//可以根据不同的分区返回每个分区的行数   

 return 6;

}

#pragma mark-第二个必须实现的方法,用来设置cell

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

//cell的创建    

//设置重用标示,一般来讲,一个种类的cell就设置一个对应的重用标示.    

static NSString *reuse = @"cell";   

 //根据指定的重用标示,在重用池中找,是否有闲置的cell    

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];    

//如果找到了闲置的cell,对象就不是空的,如果没找到,cell保持了一个空值,我们就要创建一个新的cell    

if (!cell) {        

cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse]autorelease];    

}    

cell.textLabel.text = @"测试";    

cell.detailTextLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];    cell.imageView.image = [UIImage imageNamed:@"c1a3c815gw1f3k9acvbihj206o053jrq.jpg"];    

return  cell;

}

#pragma mark-tableView的协议方法,可以写可以不写(默认是1),设置分区的个数

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    

return 3;

}

#pragma mark-tableView的协议方法,可以写可以不写,设置分区的标题

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{    

if (section == 0) {        

return @"class1";    

}else if (section == 1){        

return @"class2";    

}else{        

return @"class3";    

}}

#pragma mark-tableView的协议方法,可以写可以不写,返回一个section标题的列表(方便快速的定位分区)-(NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView{

return @[@"A",@"B",@"C"];

}

#pragma mark-tableView的协议方法,可以写可以不写,点击cell的点击方法

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

NSLog(@"%ld", indexPath.row);

NSLog(@"%ld", indexPath.section);

}

#pragma mark-tableView的协议方法,可以写,可以不写,设置行高

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

if (indexPath.row % 2 == 0) {

return 150;

}else{

return 200;

}

}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

if (section == 0) {

return 30;

}else{

return 50;

}

}

运行结果如下:

相关文章

网友评论

      本文标题:UI总结-tableView的基本用法

      本文链接:https://www.haomeiwen.com/subject/vyvnrttx.html