UITableView - 2
来源:互联网 发布:sql server复制表结构 编辑:程序博客网 时间:2024/05/22 01:58
4.折叠(像QQ联系人展开,收起的效果):
实现方法一:
头文件:
#import <UIKit/UIKit.h>@interface ViewController : UIViewController@end#pragma mark - Customer TableViewHeaderFooterView@interface PressHeader : UITableViewHeaderFooterView@property (nonatomic, strong) UIButton* pressButton;- (instancetype)initWithFrame:(CGRect)frame andReuseIdentifier:(NSString*)reuseIdentifier andTarget:(id)target andAction:(SEL)sel andTag:(NSInteger)tag andTitle:(NSString*)title;@end
实现文件:
ConstantHeader是这些内容:
#define FOLD_STATE @"FOLD_STATE"#define cellReuseIdentifier @"cellReuseIdentifier"#define headerReuseIdentifier @"headerReuseIdentifier"
#import "ConstantHeader.h"#import "ViewController.h"@interface ViewController () <UITableViewDataSource, UITableViewDelegate>{ UITableView* tableview; NSMutableArray* dataArr; }@end@implementation ViewController- (void)viewDidLoad{ [super viewDidLoad]; NSArray* arr = @[ @{ @"魏": @[ @"张辽", @"李典", @"曹洪" ] }, @{ @"蜀": @[ @"关羽", @"张飞", @"赵云" ] }, @{ @"吴": @[ @"周瑜", @"鲁肃", @"黄盖" ] } ]; dataArr = [NSMutableArray array]; for (NSDictionary* dic in arr) { NSMutableDictionary* newDic = [dic mutableCopy]; [newDic setObject:[NSNumber numberWithBool:YES] forKey:FOLD_STATE]; [dataArr addObject:newDic]; } tableview = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain]; tableview.delegate = self; tableview.dataSource = self; [self.view addSubview:tableview]; UIView* footer = [[UIView alloc] initWithFrame:CGRectZero]; tableview.tableFooterView = footer; }#pragma mark - Obtain Key- (NSString*)obtainKey:(NSDictionary* )dic{ NSString* key = NULL; NSArray* tempArr = [dic allKeys]; for (NSString* str in tempArr) { if (![str isEqualToString:FOLD_STATE]) { key = str; } } return key;}- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning];}#pragma mark - UITableViewDelegate && UITableViewDataSource- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ NSDictionary* dic = [dataArr objectAtIndex:section]; if ([[dic objectForKey:FOLD_STATE] boolValue]) { return 0; } NSString* key = NULL; key = [self obtainKey:dic]; return [[dic objectForKey:key] count];}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return [dataArr count];}- (UITableViewCell* )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell* cell = [tableview dequeueReusableCellWithIdentifier:cellReuseIdentifier]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseIdentifier]; } NSDictionary* dic = [dataArr objectAtIndex:indexPath.section]; NSString* key = NULL; key = [self obtainKey:dic]; NSArray* cellDataArr = [dic objectForKey:key]; cell.textLabel.text = [cellDataArr objectAtIndex:indexPath.row]; return cell;}- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 44.0;}-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ NSDictionary* dic = [dataArr objectAtIndex:section]; NSString* key = NULL; key = [self obtainKey:dic]; PressHeader* pressHedaer = nil; pressHedaer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headerReuseIdentifier]; if (pressHedaer == nil) { pressHedaer = [[PressHeader alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 44) andReuseIdentifier:headerReuseIdentifier andTarget:self andAction:@selector(press:) andTag:section+100 andTitle:key]; } return pressHedaer;}-(void)press:(UIButton*)btn{ NSMutableDictionary* dic = [dataArr objectAtIndex:btn.tag-100]; NSNumber* number = [dic objectForKey:FOLD_STATE]; if ([number boolValue]) { [dic setObject:[NSNumber numberWithBool:NO] forKey:FOLD_STATE]; } else { [dic setObject:[NSNumber numberWithBool:YES] forKey:FOLD_STATE]; } //局部reload [tableview reloadSections:[NSIndexSet indexSetWithIndex:btn.tag-100] withRowAnimation:UITableViewRowAnimationFade];}@end#pragma mark - Customer TableViewHeaderFooterView@implementation PressHeader- (instancetype)initWithFrame:(CGRect)frame andReuseIdentifier:(NSString *)reuseIdentifier andTarget:(id)target andAction:(SEL)sel andTag:(NSInteger)tag andTitle:(NSString *)title{ self = [super initWithReuseIdentifier:reuseIdentifier]; if (self) { self.frame = frame; self.pressButton = [UIButton buttonWithType:UIButtonTypeCustom]; [self.pressButton setFrame:frame]; [self.pressButton setBackgroundColor:[UIColor grayColor]]; [self.pressButton setTitle:title forState:UIControlStateNormal]; [self.pressButton addTarget:target action:sel forControlEvents:UIControlEventTouchUpInside]; [self.pressButton setTag:tag]; [self.contentView addSubview:self.pressButton]; } return self;}@end
这个实现方法很简单,就是把每一个viewheader都放置一个button,然后根据状态的值来决定是否展开。
优点:简单,每一个header都能重用
注意:这个方法的本质是对数据结构的灵活运用,首先是对数据的封装,设置了一个BOOL的判断值。这个例子的不足,没有考虑如果值为空的情况,例如“越国”没有人才,这个时候会怎么样?字典数据应该怎么处理?
0 0
- UITableView - 2
- UITableView详细讲解2
- IOS-UITableView入门(2)
- iOS开发-UITableView-2
- UITableView基本使用方法2
- UITableView优化技巧2
- UITableView应用-2A
- UITableView
- UITableView
- UITableView
- UITableView
- UITableView
- UITableView
- UITableView
- UITableView
- UITableview
- UITableView
- UITableView
- hdu 4746 Mophues 莫比乌斯反演
- Eclipse安装testng失败,提示MD5值错误
- 原生sql 查clob字段,查出的结果是string类型
- 封装的AlertDialog
- 单例模式
- UITableView - 2
- 今天搞了一个下午,总算是把并查集了解了一点
- [Object-C]使用个推遇到的坑
- Redis
- 写给以后从事金融分析师学员的一些话(附行研经验)
- Spring依赖注入——java项目中使用spring注解方式进行注入
- 食物链(带权并查集)
- 比较差劲的自定义view
- OpenCV基本操作