那么今天还是跟同学举例汽车列表展示的项目,从最基本的tableView的用法到MVC思想的过度到深度封装和自定义cell以及cell的所有用法,那么废话不多说直接上代码,先看效果图
//
// ZZViewController.h
// 03-汽车品牌(MVC)
//
// Created by 周昭 on 16/10/28.
// Copyright © 2016年 HT_Technology. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ZZViewController : UIViewController
@end
//
// ZZViewController.m
// 03-汽车品牌(MVC)
//
// Created by 周昭 on 16/10/28.
// Copyright © 2016年 HT_Technology. All rights reserved.
//
#import "ZZViewController.h"
#import "ZZCarGroup.h"
@interface ZZViewController()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
#pragma mark - 这里我们的数据是不是首先就要搞个数组来承载
/**
* 承载汽车模型
*/
@property (nonatomic, strong) NSArray *carArr;
@end
@implementation ZZViewController
#pragma mark - 我们的数据通过懒加载的方式呈现出来我们不需要管他什么时候创建
- (NSArray *)carArr
{
if (_carArr == nil) {
// 初始化
// 德系品牌
ZZCarGroup *cg1 = [[ZZCarGroup alloc] init];
cg1.title = @"德系品牌";
cg1.desc = @"德系品牌很好";
cg1.cars = @[@"奥迪", @"宝马", @"奔驰", @"奥迪",@"奥迪", @"宝马", @"奔驰", @"奥迪",@"奥迪", @"宝马", @"奔驰", @"奥迪"];
// 日韩品牌
ZZCarGroup *cg2 = [[ZZCarGroup alloc] init];
cg2.title = @"日韩品牌";
cg2.desc = @"日韩品牌很好haohaohao";
cg2.cars = @[@"本田", @"丰田", @"本田", @"丰田",@"本田", @"丰田", @"本田", @"丰田",@"本田", @"丰田", @"本田", @"丰田",@"本田", @"丰田", @"本田", @"丰田"];
// 欧系品牌
ZZCarGroup *cg3 = [[ZZCarGroup alloc] init];
cg3.title = @"欧系品牌";
cg3.desc = @"欧系品牌goodgood";
cg3.cars = @[@"兰博基尼", @"法拉利",@"兰博基尼", @"法拉利",@"兰博基尼", @"法拉利",@"兰博基尼", @"法拉利",@"兰博基尼", @"法拉利",@"兰博基尼", @"法拉利",@"兰博基尼", @"法拉利",@"兰博基尼", @"法拉利",@"兰博基尼", @"法拉利",@"兰博基尼", @"法拉利",@"兰博基尼", @"法拉利",@"兰博基尼", @"法拉利"];
_carArr = @[cg1, cg2, cg3];
}
return _carArr;
}
/**
* 隐藏状态栏
*/
- (BOOL)prefersStatusBarHidden
{
return YES;
}
#pragma mark - 数据源方法
/**
* 一共有多少组数据
*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.carArr.count;
}
/**
* 第section组有多少行
*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// 取得第section组对应的模型
ZZCarGroup *cg = self.carArr[section];
return cg.cars.count;
}
/**
* 每一行显示怎样的内容(cell)
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
// 取出第indexPath.section组对应的模型
ZZCarGroup *cg = self.carArr[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
{
ZZCarGroup *cg = self.carArr[section];
return cg.title;
}
@end
//
// ZZCarGroup.h
// 03-汽车品牌(MVC)
//
// Created by 周昭 on 16/10/28.
// Copyright © 2016年 HT_Technology. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface ZZCarGroup : NSObject
/**
* 头部标题
*/
@property (nonatomic, copy) NSString *title;
/**
* 尾部标题(描述)
*/
@property (nonatomic, copy) NSString *desc;
/**
* 这组的所有车(字符串)
*/
@property (nonatomic, strong) NSArray *cars;
@end
//
// ZZCarGroup.m
// 03-汽车品牌(MVC)
//
// Created by 周昭 on 16/10/28.
// Copyright © 2016年 HT_Technology. All rights reserved.
//
#import "ZZCarGroup.h"
@implementation ZZCarGroup
@end

本文介绍了一个使用MVC设计模式实现的汽车品牌列表展示项目。项目采用UITableView进行数据展示,并通过自定义模型ZZCarGroup来组织不同系列的汽车品牌数据。
232

被折叠的 条评论
为什么被折叠?



