UITableView 表视图

来源:互联网 发布:旅游软件有哪些 编辑:程序博客网 时间:2024/05/16 16:56

UITableView 继承自UIScrollView, 可以进行 分区(section),行(row), frame 决定tableView显示的位置,边框,

row表示tableView中某一行的位置,数量决定contentSize, tableView根据行数自行设置, 每一行的位置都会放置一个UITableViewCell负责显示文本。

cell 有重用机制,cell作为显示数据的。这个重用机制就是说,如果有1000个row,就需要1000个cell,这样太浪费, 可以吧显示到屏幕上cell数量j

进行重用,随着滑动,每次都把数据显示到这几个cell中。

UITableView 属性

Style,  分割线样式,分割线颜色, 行高, 控制代理, 数据代理。

- (void)viewDidLoad {

    [superviewDidLoad];

    [self.viewsetBackgroundColor:[UIColorwhiteColor]];

    UITableView *tView = [[UITableViewalloc] initWithFrame:CGRectMake(0,0, 390,self.view.frame.size.height)style:UITableViewStylePlain];

    //设置行高

    [tViewsetRowHeight:80.0];

    //设置分割线颜色

    [tView setSeparatorColor:[UIColorblueColor]];

    //设置cell的样式

    [tView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLineEtched];

    

    [self.viewaddSubview:tView];

    // Do any additional setup after loading the view, typically from a nib.

}


UITableView有两个协议, DataSource, Delegate。

UITableViewDataSource

有三个方法

一个可选的

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

两个必须实现的

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

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:

// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

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




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    //返回多少个section, 如果返回1, 就只有一个,这里返回四个

   return 4;

}


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

{

//每个section 返回多少行

   if (section == 0) {

       return 2;

    }

   if (section == 1) {

       return 2;

    } 

    if(section == 2) {

return 3;

    }else {

       return 3;

    }

}


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

{

    //cell标识

   static NSString *cellIdentifier =@"cell";

    //查备用队列

   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    //队列里没有,就从新创建一个cell

   if (!cell) {

        cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellIdentifier];

    }

    // 在每一个section上显示数据,

    cell.textLabel.text = [NSStringstringWithFormat:@"section = %d, row = %d", indexPath.section, indexPath.row];

   return cell;

}



- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end



使用代理方法进行设置行高等


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

{

    

   if (indexPath.section ==0) {

       return 40;

    }

   if (indexPath.section ==1) {

       if (indexPath.row ==0) {

           return 40;

        }

       return 60;

    }

   return 50;

}









- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

{

   return [NSArrayarrayWithObjects:@"1",@"2", @"3",@"4", nil];

}


//设置section

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

{

   return [NSStringstringWithFormat:@"%ld", section+1];

    //return @"HEAD TEST";

}


//设置section尾;

- (NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

{

    return@"FOOTER TEST";

}




0 0