UITableView

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属性

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值