當前位置:
首頁 >
UITableView的UITableViewStyleGrouped
發布時間:2024/9/5
37
豆豆
生活随笔
收集整理的這篇文章主要介紹了
UITableView的UITableViewStyleGrouped
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
UITableView的UITableViewStyleGrouped
以下圖例就是分組UITableViewStyleGrouped的樣式
本人提供快速集成的方法,不弄臟你那雙手:)
源碼:
TableViewData.h
// // 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:(NSInteger)section;// 對應于indexPath中的數據 - (id)dataInIndexPath:(NSIndexPath *)indexPath;@endTableViewData.m
// // 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:(NSInteger)section {return _nameList[section]; }- (id)dataInIndexPath:(NSIndexPath *)indexPath {return _dataArray[indexPath.section][indexPath.row]; }@end使用情況:
// // RootViewController.m // UITableViewGroup // // Copyright (c) 2014年 Y.X. All rights reserved. // #import "RootViewController.h" #import "TableViewData.h"static NSString *reusedFlag = @"reusedFlag";@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>@property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) TableViewData *tableViewData;@end@implementation RootViewController- (void)viewDidLoad {[super viewDidLoad];// 初始化tableView(分組形勢的tableView)_tableView = [[UITableView alloc] initWithFrame:self.view.boundsstyle:UITableViewStyleGrouped];_tableView.delegate = self;_tableView.dataSource = self;[self.view addSubview:_tableView];// tableView數據_tableViewData = [TableViewData new];[_tableViewData addDataArray:@[@"1", @"2", @"3"] arrayFlag:@"設置"]; // section1[_tableViewData addDataArray:@[@"一", @"二", @"三"] arrayFlag:@"配置"]; // section2[_tableViewData addDataArray:@[@"one", @"two", @"three"] arrayFlag:@"閑置"]; // section3 }// 每個區多少個cell - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return [_tableViewData numberOfRowsInSection:section]; }// 創建cell - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusedFlag];if (cell == nil){cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:reusedFlag];}// section1if ([[_tableViewData flagInSection:indexPath.section] isEqualToString:@"設置"]){NSString *str = [_tableViewData dataInIndexPath:indexPath];cell.textLabel.text = str;cell.textLabel.textColor = [UIColor redColor];}// section2if ([[_tableViewData flagInSection:indexPath.section] isEqualToString:@"配置"]){NSString *str = [_tableViewData dataInIndexPath:indexPath];cell.textLabel.text = str;cell.textLabel.textColor = [UIColor blueColor];}// section3if ([[_tableViewData flagInSection:indexPath.section] isEqualToString:@"閑置"]){NSString *str = [_tableViewData dataInIndexPath:indexPath];cell.textLabel.text = str;cell.textLabel.textColor = [UIColor magentaColor];}return cell; }// 多少個區 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {return [_tableViewData numberOfSections]; }// 每個區的標題 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {return [_tableViewData flagInSection:section]; }@end幾個需要注意的地方:
以下兩個方法是DataSource中的,直接與分組有關.
不同的section配置不同的標題.
看起來應該很直白:)
?
?
?
?
?
?
?
?
?
轉載于:https://www.cnblogs.com/YouXianMing/p/3944866.html
總結
以上是生活随笔為你收集整理的UITableView的UITableViewStyleGrouped的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: RDLC报表系列(二) 行分组
- 下一篇: Qt入门(3)——信号和槽