UITableView

来源:互联网 发布:相册模板制作软件 编辑:程序博客网 时间:2024/06/04 18:56

表视图用于显示数据列表,数据列表中得每项都由行表示。ios没有行数限制,行数仅受可用存储空间的限制。表视图的表只有一列时,用户可以通过上下滑动屏幕的方式来查看更多地信息。

表视图的两种类型:

  1. 分组表:UITableViewStyleGrouped
  2. 无格式表:UITableViewStylePlain

表视图的Section和Row:

表中得每个部分成为数据源中得分区(Section)。在分组中,每个分组都是一个分区。在索引表中,数据的每个索引分组都是一个分区。

表中的每个可见行对应数据源中的行(Row)。

表视图的单元格:

表视图是显示表数据的视图对象,它是UITableView类的一个实例。表中的每个可见 行都是UITableViewCell类的实例。其中UITableViewCell的实例就是单元格。

表视图的DataSource和Delegate:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

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

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

单元格内容(默认)包括图像imageView、文本标签textLable、详细文本标签detailTextLable。

单元格的样式(UITbaleViewCellStyle)

UITableViewCellStyleSubtitle

UITableViewCellStyleDefault

UITableViewCellStyleValue1

UITableViewCellStyleValue2


#import <UIKit/UIKit.h>


@interface ViewController :UIViewController<UITableViewDataSource,UITableViewDelegate>//添加两个代理


@property  (retain,nonatomic)UITableView *myTbl;//将表视图设置成属性


@end


#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad

{

    [superviewDidLoad];

    

    //创建表视图及表视图的格式UITableViewStyleGrouped

self.myTbl  = [[UITableViewalloc]initWithFrame:CGRectMake(0,0,320,460)style:UITableViewStyleGrouped];

    

    //添加到父视图

    [self.viewaddSubview:self.myTbl];

    

    //设置代理

    self.myTbl.dataSource =self;

    self.myTbl.delegate =self;

    

}


//设置表视图的分组

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 2;

}


//设置每个分组中得行数

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

{

    return 10;

}


//设置表视图中单元格的内容

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

{

    //定义静态标识符

    static NSString *identfire =@"cellIdentfire";

    //单元格重用

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identfire];

    if (cell == nil) {

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

    }

    

    //填写cell里的内容

    

    //单元格的样式

    cell.accessoryType =UITableViewCellStyleSubtitle;

    //返回单元格的对象

    return cell;

}


//单元格高度自适应

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

{

    

    NSArray *arr = nil;//具体看情况

    

    NSString *str = [arr objectAtIndex:indexPath.row];

    CGSize  size = [strsizeWithFont:[UIFontsystemFontOfSize:14]constrainedToSize:CGSizeMake(300,1000)];

    return size.height +20;

    

}


- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end