快速设置UITableView不同section对应于不同种类的cell
生活随笔
收集整理的這篇文章主要介紹了
快速设置UITableView不同section对应于不同种类的cell
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
快速設(shè)置UITableView不同section對應(yīng)于不同種類的cell
本文主要是為了寫明如何在UITableView中,一個section對應(yīng)于一種類型的cell,寫起來不凌亂.
在不封裝任何類的前提下提供如下源碼:
請自行創(chuàng)建出3種類型的cell,創(chuàng)建好了就行,你需要創(chuàng)建出ModelOneCell,ModelTwoCell,ModelThreeCell,內(nèi)容為空
// // 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; // 數(shù)據(jù)數(shù)組 @property (nonatomic, strong) NSMutableArray *nameList; // 數(shù)組名字@end@implementation RootViewController#pragma mark - 只初始化一次 #define REUESED_SIZE 100 static NSString *reUsedStr[REUESED_SIZE] = {nil}; // 重用標(biāo)示 #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.boundsstyle:UITableViewStylePlain];[self.view addSubview:_tableView];_tableView.delegate = self;_tableView.dataSource = self;// 模擬三種類型的數(shù)據(jù)源NSArray *type1 = @[@"1", @"2", @"3"];NSArray *type2 = @[@"一", @"二", @"三"];NSArray *type3 = @[@"one", @"two", @"three"];// 添加數(shù)據(jù)源 + 數(shù)據(jù)源標(biāo)簽名字_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 // 每個區(qū)有幾個cell - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return [_dataArray[section] count]; }// 設(shè)定tableView有幾個區(qū)域 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {return [_nameList count]; }// cell的初始化以及重用設(shè)置 -(UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath {// 根據(jù)section區(qū)域獲取幾種cell的公共父類UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reUsedStr[indexPath.section]];// 根據(jù)不同的區(qū)域?qū)?yīng)創(chuàng)建出該區(qū)域的cellif (cell == nil){if ([_nameList[indexPath.section] isEqualToString:@"ModelOneCell"]){cell = [[ModelOneCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:reUsedStr[indexPath.section]];}else if ([_nameList[indexPath.section] isEqualToString:@"ModelTwoCell"]){cell = [[ModelTwoCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:reUsedStr[indexPath.section]];}else if ([_nameList[indexPath.section] isEqualToString:@"ModelThreeCell"]){cell = [[ModelThreeCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:reUsedStr[indexPath.section]];}}// 對cell進行設(shè)置if ([_nameList[indexPath.section] isEqualToString:@"ModelOneCell"]){cell = [[ModelOneCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:reUsedStr[indexPath.section]];cell.textLabel.text = _dataArray[indexPath.section][indexPath.row];}else if ([_nameList[indexPath.section] isEqualToString:@"ModelTwoCell"]){cell = [[ModelTwoCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:reUsedStr[indexPath.section]];cell.textLabel.text = _dataArray[indexPath.section][indexPath.row];}else if ([_nameList[indexPath.section] isEqualToString:@"ModelThreeCell"]){cell = [[ModelThreeCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:reUsedStr[indexPath.section]];cell.textLabel.text = _dataArray[indexPath.section][indexPath.row];}return cell; }// 點擊cell獲取數(shù)據(jù) - (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]);} }// 設(shè)定不同種類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運行時候的效果如下:
核心思想:
接下來,我們就要來進行封裝,達到好用的目的:)
我們把數(shù)據(jù)源以及數(shù)據(jù)源標(biāo)簽抽象成一個對象就可以很好的管理這些東西了,以下給出源碼:
// // TableVewData.h // Sections // // Copyright (c) 2014年 Y.X. All rights reserved. // #import <Foundation/Foundation.h>@interface TableViewData : NSObject// 添加數(shù)據(jù)源 + 數(shù)據(jù)源標(biāo)簽 - (void)addDataArray:(NSArray *)array arrayFlag:(NSString *)flag;// 對應(yīng)區(qū)域中的row的個數(shù) - (NSInteger)numberOfRowsInSection:(NSInteger)section;// 有幾個section - (NSInteger)numberOfSections;// 對應(yīng)于Section上的flag值標(biāo)簽 - (NSString *)flagInSection:(NSIndexPath *)indexPath;// 對應(yīng)于indexPath中的數(shù)據(jù) - (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主函數(shù)使用情形如下:
// // 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}; // 重用標(biāo)示 #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.boundsstyle:UITableViewStylePlain];[self.view addSubview:_tableView];_tableView.delegate = self;_tableView.dataSource = self;// 模擬三種類型的數(shù)據(jù)源NSArray *type1 = @[@"1", @"2", @"3"];NSArray *type2 = @[@"一", @"二", @"三"];NSArray *type3 = @[@"one", @"two", @"three"];// 添加數(shù)據(jù)源 + 數(shù)據(jù)源標(biāo)簽名字_tableData = [TableViewData new];[_tableData addDataArray:type1 arrayFlag:@"ModelOneCell"];[_tableData addDataArray:type2 arrayFlag:@"ModelTwoCell"];[_tableData addDataArray:type3 arrayFlag:@"ModelThreeCell"]; }#pragma mark - UITableView'delegate & dataSource // 每個區(qū)有幾個cell - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return [_tableData numberOfRowsInSection:section]; }// 設(shè)定tableView有幾個區(qū)域 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {return [_tableData numberOfSections]; }// cell的初始化以及重用設(shè)置 -(UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath {// 根據(jù)section區(qū)域獲取幾種cell的公共父類UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reUsedStr[indexPath.section]];// 根據(jù)不同的區(qū)域?qū)?yīng)創(chuàng)建出該區(qū)域的cellif (cell == nil){if ([[_tableData flagInSection:indexPath] isEqualToString:@"ModelOneCell"]){cell = [[ModelOneCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:reUsedStr[indexPath.section]];}else if ([[_tableData flagInSection:indexPath] isEqualToString:@"ModelTwoCell"]){cell = [[ModelTwoCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:reUsedStr[indexPath.section]];}else if ([[_tableData flagInSection:indexPath] isEqualToString:@"ModelThreeCell"]){cell = [[ModelThreeCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:reUsedStr[indexPath.section]];}}// 對cell進行設(shè)置if ([[_tableData flagInSection:indexPath] isEqualToString:@"ModelOneCell"]){cell = [[ModelOneCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:reUsedStr[indexPath.section]];cell.textLabel.text = [_tableData dataInIndexPath:indexPath];}else if ([[_tableData flagInSection:indexPath] isEqualToString:@"ModelTwoCell"]){cell = [[ModelTwoCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:reUsedStr[indexPath.section]];cell.textLabel.text = [_tableData dataInIndexPath:indexPath];}else if ([[_tableData flagInSection:indexPath] isEqualToString:@"ModelThreeCell"]){cell = [[ModelThreeCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:reUsedStr[indexPath.section]];cell.textLabel.text = [_tableData dataInIndexPath:indexPath];}return cell; }// 點擊cell獲取數(shù)據(jù) - (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]);} }// 設(shè)定不同種類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添加數(shù)據(jù)源:
見名知意:
使用很便利:
?
轉(zhuǎn)載于:https://www.cnblogs.com/YouXianMing/p/3896336.html
總結(jié)
以上是生活随笔為你收集整理的快速设置UITableView不同section对应于不同种类的cell的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。