1. 程式人生 > >ios初識UITableView及簡單用法二(模型數據)

ios初識UITableView及簡單用法二(模型數據)

ati ins int class test tab 所有 super viewdata

技術分享

//
//  ViewController.m
//  ZQRTableViewTest
//
//  Created by zzqqrr on 17/8/24.
//  Copyright (c) 2017年 zzqqrr. All rights reserved.
//

#import "ViewController.h"
#import "ZQRCarGroup.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; } - (NSArray *)carGroups { if(_carGroups==nil){ ZQRCarGroup *model1=[[ZQRCarGroup alloc] init]; model1.title=@"哈哈"; model1.desc=@"
呵呵"; model1.cars[email protected][@"第一組第一行"]; ZQRCarGroup *model2=[[ZQRCarGroup alloc] init]; model2.title=@"哈哈"; model2.desc=@"呵呵"; model2.cars[email protected][@"第二組第一行",@"第二組第二行"]; ZQRCarGroup *model3=[[ZQRCarGroup alloc] init]; model3.title
=@"哈哈"; model3.desc=@"呵呵"; model3.cars[email protected][@"第三組第一行",@"第三組第二行",@"第三組第三行"]; _carGroups=@[model1,model2,model3]; } return _carGroups; } /** 設置多少組組 */ - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.carGroups.count; } /** 指定組中的行 */ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { ZQRCarGroup *group=self.carGroups[section]; return group.cars.count; } /** 每一組顯示的內容 */ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; ZQRCarGroup *group=self.carGroups[indexPath.section]; NSString *strCar=group.cars[indexPath.row]; cell.textLabel.text=strCar; return cell; } /** 頭部文字 */ -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { ZQRCarGroup *model=self.carGroups[section]; return model.title; } /** 尾部文字 */ - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { ZQRCarGroup *model=self.carGroups[section]; return model.desc; } @end

模型

//
//  ZQRCarGroup.h
//  
//
//  Created by zzqqrr on 17/8/25.
//
//

#import <Foundation/Foundation.h>

@interface ZQRCarGroup : NSObject
/**
 *頭部標題
 */
@property (nonatomic,copy) NSString *title;
/**
 *尾部的描述
 */
@property (nonatomic,copy) NSString *desc;
/**
 *所有的信息列表
 */
@property (nonatomic,strong) NSArray *cars;
@end
//
//  ZQRCarGroup.m
//  
//
//  Created by zzqqrr on 17/8/25.
//
//

#import "ZQRCarGroup.h"

@interface ZQRCarGroup()

@end

@implementation ZQRCarGroup

@end

ios初識UITableView及簡單用法二(模型數據)