IOS tableView的基本使用

 tableView  Style:Plain(头部标题 向上移 不会消失)

tableView  Style:Grouped(头部标题 向上移 会 消失)

 

 

 

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

@interface ViewController ()<UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property(nonatomic,strong) NSArray *carGroups;

@end

@implementation ViewController

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

//隐藏状态栏
-(BOOL)prefersStatusBarHidden
{
    return  YES;
}

-(NSArray *)carGroups
{
   if(_carGroups==nil)
   {
       //初始化
       //德系品牌
       carGroup *car1=[[carGroup alloc]init];
       car1.title=@"德系品牌";
       car1.desc=@"德系品牌很好";
       car1.cars=@[@"奥迪", @"宝马", @"奔驰",];
       //日系品牌
       carGroup *car2=[[carGroup alloc]init];
       car2.title=@"日系品牌";
       car2.desc=@"日系品牌很好sssss";
       car2.cars=@[@"本田", @"丰田"];
       //欧系品牌
       carGroup *car3=[[carGroup alloc]init];
       car3.title=@"欧系品牌";
       car3.desc=@"欧系品牌很好yyyyyy";
       car3.cars=@[@"法拉力", @"兰博基尼",];
       _carGroups=@[car1,car2,car3];
   }
    return _carGroups;
}

/**一共有多少组数据*/
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return self.carGroups.count;
}

/**第section组有多少行*/
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //取得第section级对应的模型
    carGroup *cg=self.carGroups[section];
    return cg.cars.count;
}

/**每一行显示怎样的内容(cell)*/
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    UITableViewCell *cell=[[UITableViewCell alloc]initwithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    
    UITableViewCell *cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    
    //取出 模型indexpath.section组对应的模型
    carGroup *cg=self.carGroups[indexPath.section];
    //取车第indexpath.row这行对应的品牌名称
    NSString *car=cg.cars[indexPath.row];
    
    //设置cell显示的文字
    cell.textLabel.text=car;
    
    return cell;
}

/**第section组显示怎样的头部标题*/
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    carGroup *cg=self.carGroups[section];
    return cg.title;
}
/**第section组显示怎样的尾部标题*/
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    carGroup *cg=self.carGroups[section];
    return cg.desc;

}

 

posted on 2017-02-23 09:59  守望星空  阅读(255)  评论(0编辑  收藏  举报

导航