第一次在簡書上寫文章,一點(diǎn)點(diǎn)積累,牢記實(shí)際開發(fā)和項(xiàng)目中遇到的問題和解決方法,見證自己!
上次在項(xiàng)目里遇到一個(gè)需求,要求按類型分組顯示不同的設(shè)備信息,實(shí)現(xiàn)點(diǎn)擊類型可以展開/縮回分組,網(wǎng)上雖然有方法,但是都是比較零散的,在這里自己寫了一個(gè)。</br>
我們來分析一下,需要哪些成員,
- 首先,我們需要一個(gè)字典_sectionDetailDict,用于保存分組內(nèi)容,key為分組類型;value為分組包含的內(nèi)容,是一個(gè)數(shù)組;
- 其次,我們需要一個(gè)字典_sectionOpenDict,用于保存分組展開/縮回信息;
- 最后,我們需要一個(gè)數(shù)組_sectionArray,用于保存保存類型信息,可以對該數(shù)組進(jìn)行排序,進(jìn)而按照你想要的順序顯示。
在這里,我們盡量使用了OC里自帶的字典和數(shù)組,實(shí)際上,方法不止一個(gè),我就曾經(jīng)試過自定義一個(gè)對象,成員包含分組的展開/縮回狀態(tài),以及分組包含的內(nèi)容信息,然后再以一個(gè)數(shù)組來存放該對象的實(shí)例,實(shí)際上的效果也還是可以的,缺點(diǎn)就是,在獲取對應(yīng)cell的信息時(shí),需要循環(huán)去獲取,不是很方便,使用現(xiàn)在這個(gè)方法的好處就是最大的利用了字典的便利。
基于上面的分析,我先定義了三個(gè)成員,
@interface MCDropdownListViewController ()<UITableViewDataSource,UITableViewDelegate>
{
UITableView* _tableV;//列表
NSDictionary* _sectionDetailDict;//分組詳細(xì)信息,key為當(dāng)前分組頭信息,必須是唯一的;value為數(shù)組,包含分組下的信息
NSMutableDictionary* _sectionOpenDict;//分組展開/縮回信息
NSArray* _sectionOrderArray;//分組順序信息,包含分組順序信息
}
接下來就實(shí)現(xiàn)UITableViewDataSource和UITableViewDelegate,這里只po出主要方法的實(shí)現(xiàn),完整代碼可在文章底部的鏈接中獲取,
- UITableViewDataSource
我自定義了一個(gè)類型,用來表示當(dāng)前分組的展開/縮回狀態(tài)
//自定義一個(gè)類型,用于表示列表的展開/縮回狀態(tài)
typedef NS_ENUM(NSUInteger,MCDropdownListSectionStatu) {
MCDropdownListSectionStatuOpen = 1,
MCDropdownListSectionStatuClose = 0,
};
個(gè)人覺得,最好把代碼中沒有特殊意義的數(shù)字和字符想辦法以可讀性高的方式呈現(xiàn)出來,必要時(shí),還可以放在.h文件中,模仿蘋果的風(fēng)格!
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//先取section信息
NSNumber* key = [_sectionOrderArray objectAtIndex:section];
//再取section的展開/縮回狀態(tài),展開時(shí),返回實(shí)際的分組數(shù)量;縮回時(shí),返回0,以實(shí)現(xiàn)不展開
MCDropdownListSectionStatu openOrNot = [[_sectionOpenDict objectForKey:key] unsignedIntegerValue];
if (openOrNot == MCDropdownListSectionStatuOpen) {
//列表展開,取分組實(shí)際數(shù)量,并返回
NSArray* arr = [_sectionDetailDict objectForKey:key];
return arr.count;
}
return 0;
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView* headerView = nil;
headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, HeightForHeader)];
UIButton* btn = [[UIButton alloc] initWithFrame:headerView.frame];
[headerView addSubview:btn];
//分組名
UILabel* sectionNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, (btn.bounds.size.width-10)*2/3, btn.bounds.size.height)];
sectionNameLabel.text = [_sectionOrderArray objectAtIndex:section];
[btn addSubview:sectionNameLabel];
//分組下Cell的數(shù)量
UILabel* qtyLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, (btn.bounds.size.width-10)/3, btn.bounds.size.height)];
qtyLabel.center = CGPointMake(tableView.bounds.size.width-10-qtyLabel.bounds.size.width/2, btn.bounds.size.height/2);
qtyLabel.text = [NSString stringWithFormat:@"%d個(gè)",(int)[[_sectionDetailDict objectForKey:[_sectionOrderArray objectAtIndex:section]] count]];
qtyLabel.textAlignment = NSTextAlignmentRight;
[btn addSubview:qtyLabel];
//添加點(diǎn)擊處理
[btn addTarget:self action:@selector(onExpandSection:) forControlEvents:UIControlEventTouchUpInside];
//標(biāo)記,用于處理點(diǎn)擊事件
btn.tag = section;
//
return headerView;
}
- action
實(shí)現(xiàn)onExpandSection方法
-(void)onExpandSection:(UIButton*)button
{
//獲取分組信息
NSString* section = [_sectionOrderArray objectAtIndex:button.tag];
//獲取分組展開/縮回的狀態(tài)
MCDropdownListSectionStatu openOrNot = [[_sectionOpenDict objectForKey:section] unsignedIntegerValue];
if (MCDropdownListSectionStatuClose == openOrNot) {
NSLog(@"點(diǎn)擊事件,展開分組:%@",section);
//原先是縮回的,現(xiàn)在展開
[_sectionOpenDict setObject:[NSNumber numberWithUnsignedInteger:MCDropdownListSectionStatuOpen] forKey:section];
}else {
NSLog(@"點(diǎn)擊事件,縮回分組:%@",section);
//原先是展開的,現(xiàn)在縮回
[_sectionOpenDict setObject:[NSNumber numberWithUnsignedInteger:MCDropdownListSectionStatuClose] forKey:section];
}
//刷新列表,展示結(jié)果(刷新單獨(dú)的section會(huì)有卡頓的情況,我這直接改用刷新整個(gè)列表)
// [_tableV reloadSections:[NSIndexSet indexSetWithIndex:button.tag] withRowAnimation:UITableViewRowAnimationNone];
[_tableV reloadData];
}
效果:

QQ20160903-0.png
總結(jié),方法不是唯一的,希望自己能有更多不同的想法,時(shí)間允許的話,也想實(shí)踐一下,這里還有很多不足的地方,功能也是很簡單,后續(xù)嘗試一些新功能進(jìn)去。第一次寫文章,寫的有問題的地方歡迎指正!
代碼鏈接:https://github.com/HaloMartin/DropdownList