前言
之前看到網上有很多類似於QQ分組的cell折疊的效果,但是很少有封裝好的。這里借鑒了網上的一些資料,嘗試着封裝了一個簡單易的YUFoldingTableView,不用自己再去實現具體邏輯,可快速實現tableView的cell折疊效果,使用方式和UItableView差不多,這里提供了兩種簡單的樣式
demo下載
點擊下載demo源代碼
demo效果演示
使用步驟
1.導入頭文件,遵守協議
#import "ViewController.h"
#import "YUFoldingTableView.h"
@interface ViewController () <YUFoldingTableViewDelegate>
@property (nonatomic, weak) YUFoldingTableView *foldingTableView;
@end
2.創建YUFoldingTableView
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;
3.實現YUFoldingTableView的代理,用法和UItableView類似
#pragma mark - YUFoldingTableViewDelegate / required(必須實現的代理)
// 返回箭頭的位置
- (YUFoldingSectionHeaderArrowPosition)perferedArrowPositionForYUFoldingTableView:(YUFoldingTableView *)yuTableView
{
return YUFoldingSectionHeaderArrowPositionLeft;
}
- (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;
}
- (NSString *)yuFoldingTableView:(YUFoldingTableView *)yuTableView titleForHeaderInSection:(NSInteger)section
{
return [NSString stringWithFormat:@"Title %ld",section];
}
- (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;
}
- (void )yuFoldingTableView:(YUFoldingTableView *)yuTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[yuTableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark - YUFoldingTableViewDelegate / optional (可選擇實現的)
- (NSString *)yuFoldingTableView:(YUFoldingTableView *)yuTableView descriptionForHeaderInSection:(NSInteger )section
{
return @"detailText";
}
demo下載
點擊下載demo源代碼
總結
使用還是很方便的吧,代理方法也基本按照UItableView的代理去寫的。另外,代碼里面肯定還有很多問題,希望各位大神能夠指正,我會盡量去修改,謝謝。(PS:這並不完全是原創,是參考了網上很多資料寫出來的😊)