快速設置UITableView不同section對應於不同種類的cell


快速設置UITableView不同section對應於不同種類的cell

本文主要是為了寫明如何在UITableView中,一個section對應於一種類型的cell,寫起來不凌亂.

在不封裝任何類的前提下提供如下源碼:

請自行創建出3種類型的cell,創建好了就行,你需要創建出ModelOneCell,ModelTwoCell,ModelThreeCell,內容為空

//
//  RootViewController.m
//  Sections
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"
#import "ModelOneCell.h"
#import "ModelTwoCell.h"
#import "ModelThreeCell.h"

@interface RootViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView      *tableView;  // tableView


@property (nonatomic, strong) NSMutableArray   *dataArray;  // 數據數組
@property (nonatomic, strong) NSMutableArray   *nameList;   // 數組名字


@end

@implementation RootViewController

#pragma mark - 只初始化一次
#define REUESED_SIZE  100
static NSString *reUsedStr[REUESED_SIZE] = {nil}; // 重用標示
#define REUESED_FLAG  reUsedStr[0]
+ (void)initialize
{
    if (self == [RootViewController class])
    {
        for (int i = 0; i < REUESED_SIZE; i++)
        {
            reUsedStr[i] = [NSString stringWithFormat:@"GoodBoy_%d", i];
        }
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // 初始化tableView
    _tableView         = [[UITableView alloc] initWithFrame:self.view.bounds
                                                      style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.delegate       = self;
    _tableView.dataSource     = self;
    
    // 模擬三種類型的數據源
    NSArray *type1 = @[@"1", @"2", @"3"];
    NSArray *type2 = @[@"", @"", @""];
    NSArray *type3 = @[@"one", @"two", @"three"];
    
    // 添加數據源 + 數據源標簽名字
    _dataArray = [NSMutableArray new];
    _nameList  = [NSMutableArray new];
    [_dataArray addObject:type1]; [_nameList addObject:@"ModelOneCell"];
    [_dataArray addObject:type2]; [_nameList addObject:@"ModelTwoCell"];
    [_dataArray addObject:type3]; [_nameList addObject:@"ModelThreeCell"];
}

#pragma mark - UITableView'delegate & dataSource
// 每個區有幾個cell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_dataArray[section] count];
}

// 設定tableView有幾個區域
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [_nameList count];
}

// cell的初始化以及重用設置
-(UITableViewCell *)tableView:(UITableView *)tableView
        cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 根據section區域獲取幾種cell的公共父類
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reUsedStr[indexPath.section]];
    
    // 根據不同的區域對應創建出該區域的cell
    if (cell == nil)
    {
        if ([_nameList[indexPath.section] isEqualToString:@"ModelOneCell"])
        {
            cell = [[ModelOneCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:reUsedStr[indexPath.section]];
        }
        else if ([_nameList[indexPath.section] isEqualToString:@"ModelTwoCell"])
        {
            cell = [[ModelTwoCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:reUsedStr[indexPath.section]];
        }
        
        else if ([_nameList[indexPath.section] isEqualToString:@"ModelThreeCell"])
        {
            cell = [[ModelThreeCell alloc] initWithStyle:UITableViewCellStyleDefault
                                         reuseIdentifier:reUsedStr[indexPath.section]];
        }
    }
    
    // 對cell進行設置
    if ([_nameList[indexPath.section] isEqualToString:@"ModelOneCell"])
    {
        cell = [[ModelOneCell alloc] initWithStyle:UITableViewCellStyleDefault
                                   reuseIdentifier:reUsedStr[indexPath.section]];
        cell.textLabel.text = _dataArray[indexPath.section][indexPath.row];
    }
    else if ([_nameList[indexPath.section] isEqualToString:@"ModelTwoCell"])
    {
        cell = [[ModelTwoCell alloc] initWithStyle:UITableViewCellStyleDefault
                                   reuseIdentifier:reUsedStr[indexPath.section]];
        cell.textLabel.text = _dataArray[indexPath.section][indexPath.row];
    }
    
    else if ([_nameList[indexPath.section] isEqualToString:@"ModelThreeCell"])
    {
        cell = [[ModelThreeCell alloc] initWithStyle:UITableViewCellStyleDefault
                                     reuseIdentifier:reUsedStr[indexPath.section]];
        cell.textLabel.text = _dataArray[indexPath.section][indexPath.row];
    }
    
    return cell;
}

// 點擊cell獲取數據
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([_nameList[indexPath.section] isEqualToString:@"ModelOneCell"])
    {
        NSLog(@"%@", _dataArray[indexPath.section][indexPath.row]);
    }
    else if ([_nameList[indexPath.section] isEqualToString:@"ModelTwoCell"])
    {
        NSLog(@"%@", _dataArray[indexPath.section][indexPath.row]);
    }
    
    else if ([_nameList[indexPath.section] isEqualToString:@"ModelThreeCell"])
    {
        NSLog(@"%@", _dataArray[indexPath.section][indexPath.row]);
    }
}

// 設定不同種類cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([_nameList[indexPath.section] isEqualToString:@"ModelOneCell"])
    {
        return 50;
    }
    else if ([_nameList[indexPath.section] isEqualToString:@"ModelTwoCell"])
    {
        return 70;
    }
    
    else if ([_nameList[indexPath.section] isEqualToString:@"ModelThreeCell"])
    {
        return 100;
    }
    else
    {
        return 0;
    }
}

@end

運行時候的效果如下:

核心思想:

接下來,我們就要來進行封裝,達到好用的目的:)

我們把數據源以及數據源標簽抽象成一個對象就可以很好的管理這些東西了,以下給出源碼:

//
//  TableVewData.h
//  Sections
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface TableViewData : NSObject

// 添加數據源 + 數據源標簽
- (void)addDataArray:(NSArray *)array arrayFlag:(NSString *)flag;

// 對應區域中的row的個數
- (NSInteger)numberOfRowsInSection:(NSInteger)section;

// 有幾個section
- (NSInteger)numberOfSections;

// 對應於Section上的flag值標簽
- (NSString *)flagInSection:(NSIndexPath *)indexPath;

// 對應於indexPath中的數據
- (id)dataInIndexPath:(NSIndexPath *)indexPath;

@end
//
//  TableVewData.m
//  Sections
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "TableViewData.h"

@interface TableViewData ()

@property (nonatomic, strong) NSMutableArray  *dataArray;
@property (nonatomic, strong) NSMutableArray  *nameList;

@end

@implementation TableViewData

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        _dataArray = [NSMutableArray new];
        _nameList  = [NSMutableArray new];
    }
    return self;
}

- (void)addDataArray:(NSArray *)array arrayFlag:(NSString *)flag
{
    [_dataArray addObject:array];
    [_nameList  addObject:flag];
}

- (NSInteger)numberOfRowsInSection:(NSInteger)section
{
    return [_dataArray[section] count];
}

- (NSInteger)numberOfSections
{
    return [_dataArray count];
}

- (NSString *)flagInSection:(NSIndexPath *)indexPath
{
    return _nameList[indexPath.section];
}

- (id)dataInIndexPath:(NSIndexPath *)indexPath
{
    return _dataArray[indexPath.section][indexPath.row];
}

@end

主函數使用情形如下:

//
//  RootViewController.m
//  Sections
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"
#import "ModelOneCell.h"
#import "ModelTwoCell.h"
#import "ModelThreeCell.h"

#import "TableViewData.h"

@interface RootViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView      *tableView;  // tableView

@property (nonatomic, strong) TableViewData    *tableData;


@end

@implementation RootViewController

#pragma mark - 只初始化一次
#define REUESED_SIZE  100
static NSString *reUsedStr[REUESED_SIZE] = {nil}; // 重用標示
#define REUESED_FLAG  reUsedStr[0]
+ (void)initialize
{
    if (self == [RootViewController class])
    {
        for (int i = 0; i < REUESED_SIZE; i++)
        {
            reUsedStr[i] = [NSString stringWithFormat:@"GoodBoy_%d", i];
        }
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // 初始化tableView
    _tableView         = [[UITableView alloc] initWithFrame:self.view.bounds
                                                      style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.delegate       = self;
    _tableView.dataSource     = self;
    
    
    // 模擬三種類型的數據源
    NSArray *type1 = @[@"1", @"2", @"3"];
    NSArray *type2 = @[@"", @"", @""];
    NSArray *type3 = @[@"one", @"two", @"three"];
    
    // 添加數據源 + 數據源標簽名字
    _tableData = [TableViewData new];
    [_tableData addDataArray:type1 arrayFlag:@"ModelOneCell"];
    [_tableData addDataArray:type2 arrayFlag:@"ModelTwoCell"];
    [_tableData addDataArray:type3 arrayFlag:@"ModelThreeCell"];
}

#pragma mark - UITableView'delegate & dataSource
// 每個區有幾個cell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_tableData numberOfRowsInSection:section];
}

// 設定tableView有幾個區域
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [_tableData numberOfSections];
}

// cell的初始化以及重用設置
-(UITableViewCell *)tableView:(UITableView *)tableView
        cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 根據section區域獲取幾種cell的公共父類
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reUsedStr[indexPath.section]];
    
    // 根據不同的區域對應創建出該區域的cell
    if (cell == nil)
    {
        if ([[_tableData flagInSection:indexPath] isEqualToString:@"ModelOneCell"])
        {
            cell = [[ModelOneCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:reUsedStr[indexPath.section]];
        }
        else if ([[_tableData flagInSection:indexPath] isEqualToString:@"ModelTwoCell"])
        {
            cell = [[ModelTwoCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:reUsedStr[indexPath.section]];
        }
        
        else if ([[_tableData flagInSection:indexPath] isEqualToString:@"ModelThreeCell"])
        {
            cell = [[ModelThreeCell alloc] initWithStyle:UITableViewCellStyleDefault
                                         reuseIdentifier:reUsedStr[indexPath.section]];
        }
    }
    
    // 對cell進行設置
    if ([[_tableData flagInSection:indexPath] isEqualToString:@"ModelOneCell"])
    {
        cell = [[ModelOneCell alloc] initWithStyle:UITableViewCellStyleDefault
                                   reuseIdentifier:reUsedStr[indexPath.section]];
        cell.textLabel.text = [_tableData dataInIndexPath:indexPath];
    }
    else if ([[_tableData flagInSection:indexPath] isEqualToString:@"ModelTwoCell"])
    {
        cell = [[ModelTwoCell alloc] initWithStyle:UITableViewCellStyleDefault
                                   reuseIdentifier:reUsedStr[indexPath.section]];
        cell.textLabel.text = [_tableData dataInIndexPath:indexPath];
    }
    
    else if ([[_tableData flagInSection:indexPath] isEqualToString:@"ModelThreeCell"])
    {
        cell = [[ModelThreeCell alloc] initWithStyle:UITableViewCellStyleDefault
                                     reuseIdentifier:reUsedStr[indexPath.section]];
        cell.textLabel.text = [_tableData dataInIndexPath:indexPath];
    }
    
    return cell;
}

// 點擊cell獲取數據
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([[_tableData flagInSection:indexPath] isEqualToString:@"ModelOneCell"])
    {
        NSLog(@"%@", [_tableData dataInIndexPath:indexPath]);
    }
    else if ([[_tableData flagInSection:indexPath] isEqualToString:@"ModelTwoCell"])
    {
        NSLog(@"%@", [_tableData dataInIndexPath:indexPath]);
    }
    
    else if ([[_tableData flagInSection:indexPath] isEqualToString:@"ModelThreeCell"])
    {
        NSLog(@"%@", [_tableData dataInIndexPath:indexPath]);
    }
}

// 設定不同種類cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([[_tableData flagInSection:indexPath] isEqualToString:@"ModelOneCell"])
    {
        return 50;
    }
    else if ([[_tableData flagInSection:indexPath] isEqualToString:@"ModelTwoCell"])
    {
        return 70;
    }
    
    else if ([[_tableData flagInSection:indexPath] isEqualToString:@"ModelThreeCell"])
    {
        return 100;
    }
    else
    {
        return 0;
    }
}

@end

添加數據源:

見名知意:

使用很便利:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM