日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

快速设置UITableView不同section对应于不同种类的cell

發(fā)布時(shí)間:2025/7/14 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 快速设置UITableView不同section对应于不同种类的cell 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

快速設(shè)置UITableView不同section對(duì)應(yīng)于不同種類的cell

本文主要是為了寫明如何在UITableView中,一個(gè)section對(duì)應(yīng)于一種類型的cell,寫起來(lái)不凌亂.

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

請(qǐng)自行創(chuàng)建出3種類型的cell,創(chuàng)建好了就行,你需要?jiǎ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 // 每個(gè)區(qū)有幾個(gè)cell - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return [_dataArray[section] count]; }// 設(shè)定tableView有幾個(gè)區(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]];}}// 對(duì)cell進(jìn)行設(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; }// 點(diǎn)擊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

運(yùn)行時(shí)候的效果如下:

核心思想:

接下來(lái),我們就要來(lái)進(jìn)行封裝,達(dá)到好用的目的:)

我們把數(shù)據(jù)源以及數(shù)據(jù)源標(biāo)簽抽象成一個(gè)對(duì)象就可以很好的管理這些東西了,以下給出源碼:

// // 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;// 對(duì)應(yīng)區(qū)域中的row的個(gè)數(shù) - (NSInteger)numberOfRowsInSection:(NSInteger)section;// 有幾個(gè)section - (NSInteger)numberOfSections;// 對(duì)應(yīng)于Section上的flag值標(biāo)簽 - (NSString *)flagInSection:(NSIndexPath *)indexPath;// 對(duì)應(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 // 每個(gè)區(qū)有幾個(gè)cell - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return [_tableData numberOfRowsInSection:section]; }// 設(shè)定tableView有幾個(gè)區(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]];}}// 對(duì)cell進(jìn)行設(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; }// 點(diǎn)擊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ù)源:

見(jiàn)名知意:

使用很便利:

?

轉(zhuǎn)載于:https://www.cnblogs.com/YouXianMing/p/3896336.html

總結(jié)

以上是生活随笔為你收集整理的快速设置UITableView不同section对应于不同种类的cell的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。