【IOS笔记】UITableView使用例程

本文详细介绍了UITableView的基本使用方法,包括如何设置数据源、配置单元格显示内容等,并通过具体示例展示了如何利用模型实现数据和业务逻辑的分离,提高代码可维护性和扩展性。

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


备注:故事版->Style->Grouped

【1】基本用法

//
//  ViewController.m
//  UITableView的基本使用
//
//  Created by cdj on 17/9/12.
//  Copyright © 2017年 ue. All rights reserved.
//

#import "ViewController.h"

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

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.dataSource = self;
    
}

/**
 *  告诉tableView有多少组数据
 *
 *  @param tableView <#tableView description#>
 *
 *  @return <#return value description#>
 */
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}

/**
 *  告诉tableview第section组有多少行
 *
 *  @param tableView <#tableView description#>
 *  @param section   <#section description#>
 *
 *  @return <#return value description#>
 */
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {   //第0组
        return 2;
    }else if(section == 1){
        return 6;
    }else if(section == 2){
        return 6;
    }else{
        return 1;
    }
    
}

/**
 *  告诉tableView每一行显示的内容(tableView每一行都是UITableViewCell或者它的子类)
 *
 *  @param tableView <#tableView description#>
 *  @param indexPath <#indexPath description#>
 *
 *  @return <#return value description#>
 */
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    
    if(indexPath.section == 0){
        if (indexPath.row == 0) {
            cell.textLabel.text = @"通用";
        }else{
            cell.textLabel.text = @"隐私";
        }
    
    }else{
        cell.textLabel.text = [NSString stringWithFormat:@"%d %d", indexPath.section, indexPath.row];
    }

    
    
    
    return cell;

}




@end
【2】优化
//
//  ViewController.m
//  TableView展示汽车数量
//
//  Created by cdj on 17/9/12.
//  Copyright © 2017年 ue. All rights reserved.
//

#import "ViewController.h"

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

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.dataSource = self;
    
}

/**
 *  告诉tableView有多少组数据
 *
 *  @param tableView <#tableView description#>
 *
 *  @return <#return value description#>
 */
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

/**
 *  告诉tableview第section组有多少行
 *
 *  @param tableView <#tableView description#>
 *  @param section   <#section description#>
 *
 *  @return <#return value description#>
 */
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {   //第0组
        return 2;
    }else{
        return 3;
    }
    
}

/**
 *  告诉tableView每一行显示的内容(tableView每一行都是UITableViewCell或者它的子类)
 *
 *  @param tableView <#tableView description#>
 *  @param indexPath <#indexPath description#>
 *
 *  @return <#return value description#>
 */
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [[UITableViewCell alloc] init];
//    设置cell右边的指示样式
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//    在右边显示一副图片:开关
//    cell.accessoryView = [[UISwitch alloc] init];
/*
    if(indexPath.section == 0){
        if (indexPath.row == 0) {
            cell.textLabel.text = @"奔驰";
            cell.imageView.image = [UIImage imageNamed:@"m_2_100"];
        }else{
            cell.textLabel.text = @"宝马";
            cell.imageView.image = [UIImage imageNamed:@"m_3_100"];
        }
        
    }else if(indexPath.section == 1){
        if (indexPath.row == 0) {
            cell.textLabel.text = @"丰田";
            cell.imageView.image = [UIImage imageNamed:@"m_7_100"];
        }else if (indexPath.row == 1){
            cell.textLabel.text = @"马自达";
            cell.imageView.image = [UIImage imageNamed:@"m_18_100"];
        }else if (indexPath.row == 2){
            cell.textLabel.text = @"本田";
            cell.imageView.image = [UIImage imageNamed:@"m_26_100"];
        }
    }
*/
    
    NSString *name = nil;
    NSString *icon = nil;
    if(indexPath.section == 0){
        if (indexPath.row == 0) {
            name = @"奔驰";
            icon = @"m_2_100";
        }else{
            name = @"宝马";
            icon = @"m_3_100";
        }
        
    }else if(indexPath.section == 1){
        if (indexPath.row == 0) {
            name = @"丰田";
            icon = @"m_7_100";
        }else if (indexPath.row == 1){
            name = @"马自达";
            icon = @"m_18_100";
        }else if (indexPath.row == 2){
            name = @"本田";
            icon = @"m_26_100";
        }
    }
    
    cell.textLabel.text = name;
    cell.imageView.image = [UIImage imageNamed:icon];
    
    
    return cell;
    
}

/**
 *  设置tableView每一组的头部标题
 *
 *  @param tableView <#tableView description#>
 *  @param section   <#section description#>
 *
 *  @return <#return value description#>
 */
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return @"德系品牌";
    }else{
        return @"日系品牌";
    }
}

/**
 *  设置tableView每一组的尾部标题
 *
 *  @param tableView <#tableView description#>
 *  @param section   <#section description#>
 *
 *  @return <#return value description#>
 */
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return @"德系品牌-------";
    }else{
        return @"日系品牌-------";
    }
}





@end

【3】通过模型将数据和业务逻辑分离,便于扩展

//
//  ViewController.m
//  TableView展示汽车数量
//
//  Created by cdj on 17/9/12.
//  Copyright © 2017年 ue. All rights reserved.
//

#import "ViewController.h"
#import "UECarGroup.h"
#import "UECar.h"

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

/* 所有车的数据 */
@property (nonatomic, strong) NSArray *carGroups;

@end

@implementation ViewController

//懒加载
-(NSArray *)carGroups{

    if (!_carGroups) {
        UECarGroup *group0 =[[UECarGroup alloc] init];
        group0.header = @"德系品牌";
        group0.footer = @"德系品牌***";
        group0.cars =@[
                       [UECar carWithName:@"奔驰" icon:@"m_2_100"],
                       [UECar carWithName:@"宝马" icon:@"m_3_100"]
                       ];
        
        UECarGroup *group1 =[[UECarGroup alloc] init];
        group1.header = @"日系品牌";
        group1.footer = @"日系品牌------";
        group1.cars =@[
                       [UECar carWithName:@"丰田" icon:@"m_7_100"],
                       [UECar carWithName:@"马自达" icon:@"m_18_100"],
                       [UECar carWithName:@"本田" icon:@"m_26_100"]
                       ];
        
        _carGroups = @[group0, group1];
    }
    return _carGroups;
}


- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.dataSource = self;
    
}

/**
 *  告诉tableView有多少组数据
 *
 *  @param tableView <#tableView description#>
 *
 *  @return <#return value description#>
 */
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.carGroups.count;
}

/**
 *  告诉tableview第section组有多少行
 *
 *  @param tableView <#tableView description#>
 *  @param section   <#section description#>
 *
 *  @return <#return value description#>
 */
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    UECarGroup *group = self.carGroups[section];
    return group.cars.count;
    
//    if (section == 0) {   //第0组
//        return 2;
//    }else{
//        return 3;
//    }
    
}

/**
 *  告诉tableView每一行显示的内容(tableView每一行都是UITableViewCell或者它的子类)
 *
 *  @param tableView <#tableView description#>
 *  @param indexPath <#indexPath description#>
 *
 *  @return <#return value description#>
 */
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [[UITableViewCell alloc] init];
//    设置cell右边的指示样式
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//    在右边显示一副图片:开关
//    cell.accessoryView = [[UISwitch alloc] init];
/*
    if(indexPath.section == 0){
        if (indexPath.row == 0) {
            cell.textLabel.text = @"奔驰";
            cell.imageView.image = [UIImage imageNamed:@"m_2_100"];
        }else{
            cell.textLabel.text = @"宝马";
            cell.imageView.image = [UIImage imageNamed:@"m_3_100"];
        }
        
    }else if(indexPath.section == 1){
        if (indexPath.row == 0) {
            cell.textLabel.text = @"丰田";
            cell.imageView.image = [UIImage imageNamed:@"m_7_100"];
        }else if (indexPath.row == 1){
            cell.textLabel.text = @"马自达";
            cell.imageView.image = [UIImage imageNamed:@"m_18_100"];
        }else if (indexPath.row == 2){
            cell.textLabel.text = @"本田";
            cell.imageView.image = [UIImage imageNamed:@"m_26_100"];
        }
    }
*/
    /*
    NSString *name = nil;
    NSString *icon = nil;
    if(indexPath.section == 0){
        if (indexPath.row == 0) {
            name = @"奔驰";
            icon = @"m_2_100";
        }else{
            name = @"宝马";
            icon = @"m_3_100";
        }
        
    }else if(indexPath.section == 1){
        if (indexPath.row == 0) {
            name = @"丰田";
            icon = @"m_7_100";
        }else if (indexPath.row == 1){
            name = @"马自达";
            icon = @"m_18_100";
        }else if (indexPath.row == 2){
            name = @"本田";
            icon = @"m_26_100";
        }
    }
    
    cell.textLabel.text = name;
    cell.imageView.image = [UIImage imageNamed:icon];
    */
    
    UECarGroup *group = self.carGroups[indexPath.section];
    UECar *car = group.cars[indexPath.row];
    cell.textLabel.text = car.name;
    cell.imageView.image = [UIImage imageNamed:car.icon];
    
    return cell;
    
}

/**
 *  设置tableView每一组的头部标题
 *
 *  @param tableView <#tableView description#>
 *  @param section   <#section description#>
 *
 *  @return <#return value description#>
 */
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
   
    UECarGroup *group = self.carGroups[section];
    return group.header;
    
/*
    if (section == 0) {
        return @"德系品牌";
    }else{
        return @"日系品牌";
    }
 */
}

/**
 *  设置tableView每一组的尾部标题
 *
 *  @param tableView <#tableView description#>
 *  @param section   <#section description#>
 *
 *  @return <#return value description#>
 */
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
   
    UECarGroup *group = self.carGroups[section];
    return group.footer;
    /*
    if (section == 0) {
        return @"德系品牌-------";
    }else{
        return @"日系品牌-------";
    }
     */
}





@end


//
//  UECar.h
//  TableView展示汽车数量
//
//  Created by cdj on 17/9/12.
//  Copyright © 2017年 ue. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface UECar : NSObject

/* 名称 */
@property (nonatomic, copy) NSString *name;






/* 图标 */
@property (nonatomic, copy) NSString *icon;

+(instancetype) carWithName:(NSString *)name icon:(NSString *)icon;

@end

//
//  UECar.m
//  TableView展示汽车数量
//
//  Created by cdj on 17/9/12.
//  Copyright © 2017年 ue. All rights reserved.
//

#import "UECar.h"

@implementation UECar

+(instancetype) carWithName:(NSString *)name icon:(NSString *)icon{

    UECar *car = [[self alloc] init];
    car.name = name;
    car.icon = icon;
    return car;
}

@end





//
//  UECarGroup.h
//  TableView展示汽车数量
//
//  Created by cdj on 17/9/12.
//  Copyright © 2017年 ue. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface UECarGroup : NSObject

/* 头部 */
@property (nonatomic, copy) NSString *header;

/* 尾部 */
@property (nonatomic, copy) NSString *footer;

/* 所有的车 */
@property (nonatomic, copy) NSArray  *cars;

@end

//
//  UECarGroup.m
//  TableView展示汽车数量
//
//  Created by cdj on 17/9/12.
//  Copyright © 2017年 ue. All rights reserved.
//

#import "UECarGroup.h"

@implementation UECarGroup



@end

































评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值