前言
最近在簡(jiǎn)書上收獲不少餐蔬,也覺(jué)得有必要把自己整理的東西分享給大家急但。之前看到網(wǎng)上有很多類似于QQ分組的cell折疊的效果坛善,但是很少有封裝好的晾蜘。這里借鑒了網(wǎng)上的一些資料邻眷,嘗試著封裝了一個(gè)簡(jiǎn)單易的YUFoldingTableView,不用自己再去實(shí)現(xiàn)具體邏輯剔交,可快速實(shí)現(xiàn)tableView的cell折疊效果肆饶,使用方式和UItableView差不多,這里提供了兩種簡(jiǎn)單的樣式
demo下載
點(diǎn)擊下載demo源代碼
demo效果
使用步驟
1.導(dǎo)入頭文件岖常,遵守協(xié)議
#import "ViewController.h"
#import "YUFoldingTableView.h"
@interface ViewController () <YUFoldingTableViewDelegate>
@property (nonatomic, weak) YUFoldingTableView *foldingTableView;
@end
2.創(chuàng)建YUFoldingTableView
self.automaticallyAdjustsScrollViewInsets = NO;
YUFoldingTableView *foldingTableView = [[YUFoldingTableView alloc] initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64)];
_foldingTableView = foldingTableView;
[self.view addSubview:foldingTableView];
foldingTableView.foldingDelegate = self;
// 可以設(shè)置cell默認(rèn)展開驯镊,不設(shè)置的話,默認(rèn)折疊
foldingTableView.foldingState = YUFoldingSectionStateShow;
3.實(shí)現(xiàn)YUFoldingTableView的代理竭鞍,用法和UItableView類似
#pragma mark - YUFoldingTableViewDelegate / required(必須實(shí)現(xiàn)的代理)
- (NSInteger )numberOfSectionForYUFoldingTableView:(YUFoldingTableView *)yuTableView
{
return 5;
}
- (NSInteger )yuFoldingTableView:(YUFoldingTableView *)yuTableView numberOfRowsInSection:(NSInteger )section
{
return 3;
}
- (CGFloat )yuFoldingTableView:(YUFoldingTableView *)yuTableView heightForHeaderInSection:(NSInteger )section
{
return 50;
}
- (CGFloat )yuFoldingTableView:(YUFoldingTableView *)yuTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}
- (UITableViewCell *)yuFoldingTableView:(YUFoldingTableView *)yuTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = @"cellID";
UITableViewCell *cell = [yuTableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
cell.textLabel.text = [NSString stringWithFormat:@"Row %ld",indexPath.row];
return cell;
}
#pragma mark - YUFoldingTableViewDelegate / optional (可選擇實(shí)現(xiàn)的)
// 自定義sectionHeaderView
- (UIView *)yuFoldingTableView:(UITableView *)yuTableView viewForHeaderInSection:(NSInteger)section
{
static NSString *headerIdentifier = @"headerIdentifier";
YUCustomHeaderView *headerFooterView = [yuTableView dequeueReusableHeaderFooterViewWithIdentifier:headerIdentifier];
if (headerFooterView == nil) {
headerFooterView = [[YUCustomHeaderView alloc] initWithReuseIdentifier:headerIdentifier];
}
headerFooterView.contentView.backgroundColor = [UIColor colorWithRed:200/255.0 green:200/255.0 blue:200/255.0 alpha:0.2];
headerFooterView.title = [NSString stringWithFormat:@"標(biāo)題 - %ld", section];
headerFooterView.descriptionText = [NSString stringWithFormat:@"自定義的sectionHeaderView - %ld", section];
return headerFooterView;
}
- (NSString *)yuFoldingTableView:(YUFoldingTableView *)yuTableView titleForHeaderInSection:(NSInteger)section
{
return [NSString stringWithFormat:@"Title %ld",section];
}
// 返回箭頭的位置
- (YUFoldingSectionHeaderArrowPosition)perferedArrowPositionForYUFoldingTableView:(YUFoldingTableView *)yuTableView
{
return YUFoldingSectionHeaderArrowPositionLeft;
}
- (void )yuFoldingTableView:(YUFoldingTableView *)yuTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[yuTableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (NSString *)yuFoldingTableView:(YUFoldingTableView *)yuTableView descriptionForHeaderInSection:(NSInteger )section
{
return @"detailText";
}
demo下載
點(diǎn)擊下載demo源代碼
總結(jié)
使用還是很方便的吧板惑,代理方法也基本按照UItableView的代理去寫的。另外偎快,代碼里面肯定還有很多問(wèn)題冯乘,希望各位大神能夠指正,我會(huì)盡量去修改晒夹,謝謝裆馒。(PS:這并不完全是原創(chuàng),是參考了網(wǎng)上很多資料寫出來(lái)的)