Day11 TableView

1 tableView 

   1.1设置组

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.carGroups.count;
}

 1.2 设置行

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    carGroup *group = [[carGroup alloc]init];
    group = self.carGroups[section];
    return group.cars.count;
}

   1.3 设置内容

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    carGroup *group = [[carGroup alloc]init];
    group = self.carGroups[indexPath.section];
    NSString *name = group.cars[indexPath.row];
    cell.textLabel.text = name;
    return cell;
}

1.4 设置头部

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    carGroup *group = [[carGroup alloc]init];
    group = self.carGroups[section];
    return group.title;
}

1.5 设置尾部

-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    carGroup *group = [[carGroup alloc]init];
    group = self.carGroups[section];
    return group.desc;
}

1.6 tableView 样式

   1.6.1 UITableViewCellStyleDefault 单行简单样式

//
//  ViewController.m
//  tableViewDemo
//
//  Created by xin on 15/4/8.
//  Copyright (c) 2015年 Jackey. All rights reserved.
//

#import "ViewController.h"
#import "carGroup.h"

@interface ViewController ()<UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic,strong) NSArray *carGroups;
@end

@implementation ViewController

-(NSArray *)carGroups{
    if(_carGroups == nil){
        carGroup *group1 = [[carGroup alloc]init];
        group1.title = @"德系品牌";
        group1.desc = @"高端大气上档次";
        group1.cars = @[@"奥迪",@"宝马"];
        
        carGroup *group2 = [[carGroup alloc]init];
        group2.title = @"日韩品牌";
        group2.desc = @"牛逼哈哈";
        group2.cars = @[@"本田",@"丰田"];
        
        _carGroups = @[group1,group2];
    }
    
    return _carGroups;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置数据源
    self.tableView.dataSource = self;
}

#pragma mark - UITableViewDataSource
/*
 * 组
 */
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.carGroups.count;
}

/*
 * 行
 */
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    carGroup *group = [[carGroup alloc]init];
    group = self.carGroups[section];
    return group.cars.count;
}

/*
 * 设置标题
 */

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    carGroup *group = [[carGroup alloc]init];
    group = self.carGroups[section];
    return group.title;
}

/*
 * 设置foot
 */

-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    carGroup *group = [[carGroup alloc]init];
    group = self.carGroups[section];
    return group.desc;
}

/*
 * 告知系统具体的行显示的内容
 */

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    carGroup *group = [[carGroup alloc]init];
    group = self.carGroups[indexPath.section];
    NSString *name = group.cars[indexPath.row];
    cell.textLabel.text = name;
    return cell;
}
@end

ps注意点:

//1
interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

//2 
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.tableView.dataSource = self;
    //self.tableView.rowHeight = 60;
    self.tableView.delegate = self;
}

tableView 的一些属性

self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.separatorColor = [UIColor redColor];
    self.tableView.separatorColor = [UIColor colorWithRed:199/255.0 green:78/255.0 blue:96/255.0 alpha:1.0f];
    //给整个tableView的头部添加
    self.tableView.tableHeaderView = [UIButton buttonWithType:UIButtonTypeContactAdd];

  

原文地址:https://www.cnblogs.com/lihaozhou/p/4411737.html