EMAccordionTableViewController 是一个易于使用的可展开/折叠的 UITableViewDataSource 用于 iOS。它底层实现为一个 UITableViewDataSource 的代理,将 tableView:cellForRowAtIndexPath 和 tableView:didSelectRowAtIndexPath 的责任留给了代理。
更新:现在它支持带有视差效果的表头视图。我使用了 wwdc2013 中的 UIImage+ImageEffects 来支持模糊效果。
或者您可以直接复制文件夹 EMAccordionTable 中包含的所有文件,然后开始使用它!
@interface EMAccordionTableViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UIImage * closedSectionIcon;
@property (nonatomic, strong) UIImage * openedSectionIcon;
@property (nonatomic, strong) UITableView *tableView;
- (id) initWithTable:(UITableView *)tableView;
- (void) addAccordionSection: (EMAccordionSection *) section;
- (void) setDelegate: (NSObject <EMAccordionTableDelegate> *) delegate;
- (void) setHeaderHeight:(CGFloat)height;
@end
@protocol EMAccordionTableDelegate
// Setup the EMAccordionTableViewController
origin = 20.0f;
if ([[UIDevice currentDevice].model hasPrefix:@"iPad"])
origin = 100.0f;
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(origin, origin, self.view.bounds.size.width - origin*2, self.view.bounds.size.height - origin*2) style:UITableViewStylePlain];
[tableView setSectionHeaderHeight:kTableHeaderHeight];
/*
... set here some other tableView properties ...
*/
// Setup the EMAccordionTableViewController
emTV = [[EMAccordionTableViewController alloc] initWithTable:tableView withAnimationType:EMAnimationTypeBounce];
[emTV setDelegate:self];
[emTV setClosedSectionIcon:[UIImage imageNamed:@"closedIcon"]];
[emTV setOpenedSectionIcon:[UIImage imageNamed:@"openedIcon"]];
// If you want to set the parallax header view
emParallaxHeaderView = [[EMAccordionTableParallaxHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, tableView.bounds.size.width, 200.0f)];
emParallaxHeaderView.headerImage = [UIImage imageNamed:@"naples"];
emTV.parallaxHeaderView = emParallaxHeaderView;
// Setup some test data
dataSection01 = [[NSMutableArray alloc] initWithObjects:@"Dog", @"Cat", @"Pig", nil];
dataSection02 = [[NSMutableArray alloc] initWithObjects:@"Federer", @"Nadal", nil];
//
// Section graphics
UIColor *sectionsColor = [UIColor colorWithRed:62.0f/255.0f green:119.0f/255.0f blue:190.0f/255.0f alpha:1.0f];
UIColor *sectionTitleColor = [UIColor whiteColor];
UIFont *sectionTitleFont = [UIFont fontWithName:@"Futura" size:24.0f];
// Add the sections to the controller
EMAccordionSection *section01 = [[EMAccordionSection alloc] init];
[section01 setBackgroundColor:sectionsColor];
[section01 setItems:dataSection01];
[section01 setTitle:@"Animals"];
[section01 setTitleFont:sectionTitleFont];
[section01 setTitleColor:sectionTitleColor];
[emTV addAccordionSection:section01];
EMAccordionSection *section02 = [[EMAccordionSection alloc] init];
[section02 setBackgroundColor:sectionsColor];
[section02 setItems:dataSection02];
[section02 setTitle:@"Tennis players"];
[section02 setTitleColor:sectionTitleColor];
[section01 setTitleFont:sectionTitleFont];
[emTV addAccordionSection:section02];
EMAccordionSection *section03 = [[EMAccordionSection alloc] init];
[section03 setBackgroundColor:sectionsColor];
[section03 setTitle:@"Buh!"];
[section03 setTitleColor:sectionTitleColor];
[section01 setTitleFont:sectionTitleFont];
[emTV addAccordionSection:section03];
sections = [[NSArray alloc] initWithObjects:section01, section02, section03, nil];
[self.view addSubview:emTV.tableView];
如何?仅自由地分叉该项目并改进它!
如果您想联系我:Twitter: ennioma. 邮箱: [email protected]