UITableView

来源:互联网 发布:网络通信工程师 编辑:程序博客网 时间:2024/05/01 07:55

UITableView 继承自UIScrollView,所以可以滚动,但只能是纵方向上的。
UITableView由section和cell组成,填充的内容来自数据源,一般数据源由ViewController作为代理,因此需要遵循它的两个协议,分别是UITableViewDataSource 和 UITableViewDelegate。

#import "MainViewController.h"@interface MainViewController ()<UITableViewDataSource,UITableViewDelegate>{    NSMutableArray *_tableArray;}@end</span>

1. 设置UITableView在视图中,可以表现为Plain和Grouped两种风格;

2. 将UITableView的dataSource设置为ViewController,并让ViewController遵循UITableViewDataSource协议;

@implementation MainViewController
- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view.        //关闭透明度    self.navigationController.navigationBar.translucent = NO;        UITableView * view = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height - 64) style:UITableViewStylePlain];    [view setBackgroundColor:[UIColor cyanColor]];    [view setSeparatorColor:[UIColor blueColor]]; //分割线颜色    [view setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine]; //分割线样式    [view setRowHeight:80]; //行高(默认值44)    [view setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)]; //分割线上下左右边距        view.dataSource = self;    view.delegate = self;        [self.view addSubview:view];    [view release];}

3. 利用DataSource协议设置UITableView的sections有多少个,调用方法numberOfSectionsInTableView,返回sections的个数;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization                _tableArray = [[NSMutableArray alloc] init];          }    return self;}


4. 设置UITableView的每个section中有多少行,调用方法numberOfRowsInSection,返回sections的行数;

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    NSLog(@"%s",__func__);    return [_tableArray count]; //行数 //数组个数有关}

5. 设置UITableView中sections的首部标题,调用方法titleForHeaderInSection,尾部标题方法为titleForFooterInSection;

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{    NSDictionary *dic = [_tableArray objectAtIndex:section];    NSString *name = [dic objectForKey:@"name"];    return name;}

6.1 设置cell中显示的内容,调用方法cellForRowAtIndexPath。通过传入的(NSIndexPath *)indexPath来确定具体的row来给定内容,最终返回UITableViewCell类型的cell对象;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    NSLog(@"%s",__func__);    NSLog(@"section == %d,row == %d",indexPath.section,indexPath.row);        //第一步 从重用池取cell    static NSString * cellIdentify = @"cell426";    //将cell从重用池取出来    MainTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];        //判断cell是否为空 是空的就重新创建    if (!cell) {        cell = [[[MainTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"哎哟"] autorelease];    }   return cell;}

6.2 其中NSIndexPath包含了section和row,可以准确定位是哪个section中的哪个row;


6.3 取得的数据内容将传递给UITableViewCell中的textLable的text属性

0 0
原创粉丝点击