UITableView - 2

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值