UI基础-iOS黑马-UITableView1-汽车品牌demo

本文介绍了如何使用UITableView展示汽车品牌的多组数据,包括Plain和Group类型,通过封装模型类来实现分组展示,并详细说明了如何配置dataSource、delegate、cell的循环利用以及自定义Cell。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/* 掌握!!!

设置UITableView的dataSource、delegate


UITableView多组数据和单组数据的展示

UITableViewCell的常见属性

UITableView的性能优化(cell的循环利用)

自定义Cell

*/

UITableView 有两种类型 Plain 和Group

Plain 一般在没有footer的情况下使用(有头部没有关系,如果有头部的话,会在当前section中header实现滞留效果!!!)

Group  分组使用

将数据封装在模型中!!!

//
//  MJCarGroup.h
//  01-汽车品牌
//
//  Created by apple on 14-3-30.
//  Copyright (c) 2014年 itcast. All rights reserved.
//  一组数据


#import <Foundation/Foundation.h>


@interface MJCarGroup : NSObject
/**
 *  头部标题
 */
@property (nonatomic, copy) NSString *title;


/**
 *  尾部标题(描述)
 */
@property (nonatomic, copy) NSString *desc;


/**
 *  这组的所有车(字符串)
 */
@property (nonatomic, strong) NSArray *cars;
@end

//
//  MJViewController.m
//  01-汽车品牌
//
//  Created by apple on 14-3-30.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

#import "MJViewController.h"
#import "MJCarGroup.h"

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

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

@implementation MJViewController

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

// 隐藏状态栏
- (BOOL)prefersStatusBarHidden
{
<span style="white-space:pre">	</span>//iOS7开始状态栏由Controller控制
    return YES;
}

- (NSArray *)carGroups
{
    if (_carGroups == nil) {
        // 初始化
        // 德系品牌
        MJCarGroup *cg1 = [[MJCarGroup alloc] init];
        cg1.title = @"德系品牌";
        cg1.desc = @"德系品牌很好";
        cg1.cars = @[@"奥迪", @"宝马", @"奔驰", @"奥迪", @"宝马", @"奔驰", @"奥迪", @"宝马", @"奔驰", @"奥迪", @"宝马", @"奔驰"];
        
        // 日韩品牌
        MJCarGroup *cg2 = [[MJCarGroup alloc] init];
        cg2.title = @"日韩品牌";
        cg2.desc = @"日韩品牌很好haohaohao";
        cg2.cars = @[@"本田", @"丰田", @"本田", @"丰田", @"本田", @"丰田", @"本田", @"丰田", @"本田", @"丰田", @"本田", @"丰田", @"本田", @"丰田", @"本田", @"丰田", @"本田", @"丰田"];
        
        // 欧系品牌
        MJCarGroup *cg3 = [[MJCarGroup alloc] init];
        cg3.title = @"欧系品牌";
        cg3.desc = @"欧系品牌goodgood";
        cg3.cars = @[@"兰博基尼", @"法拉利"];
        
        _carGroups = @[cg1, cg2, cg3];
    }
    return _carGroups;
}

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

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

/**
 *  每一行显示怎样的内容(cell)
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    
    // 取出第indexPath.section组对应的模型
    MJCarGroup *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
{
    MJCarGroup *cg = self.carGroups[section];
    
    return cg.title;
}

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

@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值