美文网首页
折叠效果

折叠效果

作者: Silence_xl | 来源:发表于2020-11-30 10:50 被阅读0次
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface ExpandCell : UITableViewCell

@property (nonatomic, strong) UIImageView *iconImageView;
@property (nonatomic, strong) UILabel *titleLabel;

@end

NS_ASSUME_NONNULL_END
//==============================
#import "ExpandCell.h"
#import <Masonry/Masonry.h>

@implementation ExpandCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self.contentView addSubview:self.iconImageView];
        [self.contentView addSubview:self.titleLabel];
        [self p_addMasonry];
    }
    return self;
}

#pragma mark - # Private Methods
- (void)p_addMasonry {
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.centerY.mas_equalTo(self.mas_centerY);
        make.width.mas_equalTo(14);
        make.height.mas_equalTo(16);
    }];

    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.iconImageView.mas_right).mas_offset(10);
        make.top.bottom.mas_equalTo(0);
        make.right.mas_equalTo(-10);
    }];
}

#pragma mark - # Getter
- (UIImageView *)iconImageView {
    if (!_iconImageView) {
        _iconImageView = [[UIImageView alloc] init];
        _iconImageView.backgroundColor = [UIColor whiteColor];
        _iconImageView.image = [UIImage imageNamed:@"ico_list.png"];
    }
    return _iconImageView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.backgroundColor = [[UIColor orangeColor] colorWithAlphaComponent:0.3];
    }
    return _titleLabel;
}

@end
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end
//===========================
#import "ViewController.h"
#import "ExpandCell.h"

#define kCell_Height 44

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (nonatomic,strong) NSMutableArray *sectionArray;
@property (nonatomic,strong) NSMutableArray *stateArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"折叠的tableView";
    self.view.backgroundColor = [UIColor whiteColor];
    [self initDataSource];
    [self.view addSubview:self.tableView];
}

- (void)initDataSource {
    self.sectionArray  = [NSMutableArray arrayWithObjects:@"关于直销银行",
                          @"关于理财产品",
                          @"关于我的投资",
                          @"安全中心",nil];
    
    NSArray *one = @[@"关于直销银行1",@"关于直销银行2",@"关于直销银行3"];
    NSArray *two = @[@"如何购买?",@"如何赎回?",@"业务办理时间?"];
    NSArray *three = @[@"关于我的投资1",@"关于我的投资2",@"关于我的投资3"];
    NSArray *four = @[@"安全中心1",@"安全中心2",@"安全中心3",@"安全中心4"];
    
    self.dataSource = [NSMutableArray arrayWithObjects:one,two,three,four, nil];
    self.stateArray = [NSMutableArray array];
    
    for (int i = 0; i < self.dataSource.count; i++) {
        //所有的分区都是闭合
        [self.stateArray addObject:@"0"];
    }
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = 0;
        _tableView.sectionHeaderHeight = 0.0;
        _tableView.sectionFooterHeight = 0.0;
        _tableView.estimatedSectionHeaderHeight = 0.0;
        _tableView.estimatedSectionFooterHeight = 0.0;
        [_tableView registerClass:[ExpandCell class] forCellReuseIdentifier:NSStringFromClass([ExpandCell class])];
    }
    return _tableView;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([self.stateArray[section] isEqualToString:@"1"]){
        //如果是展开状态
        NSArray *array = [self.dataSource objectAtIndex:section];
        return array.count;
    }else{
        //如果是闭合,返回0
        return 0;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ExpandCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ExpandCell class])];
    cell.titleLabel.textAlignment = NSTextAlignmentLeft;
    cell.titleLabel.text = self.dataSource[indexPath.section][indexPath.row];
    cell.backgroundColor = [UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleDefault;
    cell.contentView.backgroundColor = [UIColor whiteColor];
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return self.sectionArray[section];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:CGRectMake(0, 0, self.view.frame.size.width, kCell_Height)];
    [button setTag:section+1000];
    button.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.1];
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [button setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    [button addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, button.frame.size.height-1, button.frame.size.width, 1)];
    lineView.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.2];
    [button addSubview:lineView];
    
    UIView *dotView = [[UIImageView alloc]initWithFrame:CGRectMake(10, (kCell_Height-22)/2, 22, 22)];
    dotView.backgroundColor = [[UIColor cyanColor] colorWithAlphaComponent:0.2];
    dotView.layer.cornerRadius = 11;
    dotView.layer.masksToBounds = YES;
    [button addSubview:dotView];
    
    UIImageView *directionImageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width-30, (kCell_Height-6)/2, 10, 6)];
    if ([self.stateArray[section] isEqualToString:@"0"]) {
        directionImageView.image = [UIImage imageNamed:@"ico_listdown"];
    }else if ([self.stateArray[section] isEqualToString:@"1"]) {
        directionImageView.image = [UIImage imageNamed:@"ico_listup"];
    }
    [button addSubview:directionImageView];
    
    UILabel *desLabel = [[UILabel alloc]initWithFrame:CGRectMake(45, (kCell_Height-20)/2, 200, 20)];
    [desLabel setBackgroundColor:[UIColor clearColor]];
    [desLabel setFont:[UIFont systemFontOfSize:14]];
    [desLabel setText:self.sectionArray[section]];
    [button addSubview:desLabel];
    return button;
}

- (void)buttonPress:(UIButton *)sender {
    //判断状态值
    if ([self.stateArray[sender.tag - 1000] isEqualToString:@"1"]){
        //修改
        [self.stateArray replaceObjectAtIndex:sender.tag - 1000 withObject:@"0"];
    }else{
        [self.stateArray replaceObjectAtIndex:sender.tag - 1000 withObject:@"1"];
    }
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:sender.tag-1000] withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kCell_Height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.00001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return kCell_Height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"区头 == %@ , 行 == %@",self.sectionArray[indexPath.section],(self.dataSource[indexPath.section])[indexPath.row]);
}

@end
Simulator Screen Shot - iPhone 11 Pro Max - 2020-11-30 at 10.50.34.png

相关文章

  • iOS - 图片实现多层折叠效果

    iOS - 图片实现多层折叠效果 iOS - 图片实现多层折叠效果

  • 折叠效果

  • 折叠效果

    AppBarLayout 这个玩意很好用,反正我已经在 app 里面用CoordinatorLayout+AppB...

  • iCarousel用法

    折叠效果

  • Expander

    实现折叠列表的效果 效果如下:

  • Android 一个可折叠的文本控件

    项目中有文字框可折叠的需求,即文本内容超出我们规定的行数。这时候用户可以自己进行手动折叠展开。效果如下: 折叠效果...

  • android 折叠效果

    android 折叠效果 学习笔记,代码自定义LinearLayout: 用法:在布局中直接使用。

  • 折叠效果|动画

    前言 为什么我们要做这个效果装逼炫技?没有别的了,废话少说先看看效果 下方的箭头主要是来确定,整个控件的高度,是随...

  • 图片折叠效果

    OC代码 #import"ViewController.h" @interfaceViewController()...

  • TableView 折叠效果

    公司里要做个一个Tableview的折叠效果.大概想了一下,有三种实现.1.直接建立Tableview,使用UIV...

网友评论

      本文标题:折叠效果

      本文链接:https://www.haomeiwen.com/subject/mvlrwktx.html