iOS实现列表折叠效果

 更新时间:2020年02月21日 08:36:26   作者:Robert火山  
这篇文章主要为大家详细介绍了iOS实现列表折叠效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了iOS实现列表折叠效果的具体代码,供大家参考,具体内容如下

实现列表折叠效果其实比较简单,点击列表头部的时候,把返回列表行数设为 0,就是收起列表;再次点击列表头部,显示列表的行数,就展开了列表。

#import "TableDownUpVC.h"
#import "TableViewCell_TableSelect.h"

@interface TableDownUpVC ()
{
 NSMutableDictionary *dicSelet;
 NSArray *arrData;
 NSMutableArray *arrStatus;
 NSInteger selectFlag;

 NSMutableDictionary *dictShow;
}

@property (nonatomic, strong) UIImageView *imgArror;

@end

@implementation TableDownUpVC

- (void)viewDidLoad {
 [super viewDidLoad];
 self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
 self.title = @"列表折叠效果";

 dictShow = [[NSMutableDictionary alloc] init];
 arrStatus = [[NSMutableArray alloc] init];

 NSDictionary *dict0 = @{@"section":@"头部0",
       @"content":@[@{@"title":@"Section0",@"subTitle":@"Row0",@"avator":@"user_default_blue"},
           @{@"title":@"Section0",@"subTitle":@"Row1",@"avator":@"user_default_blue"},
           @{@"title":@"Section0",@"subTitle":@"Row2",@"avator":@"user_default_blue"}]};

 NSDictionary *dict1 = @{@"section":@"头部1",
       @"content":@[@{@"title":@"Section1",@"subTitle":@"Row0",@"avator":@"user_default_blue"},
           @{@"title":@"Section1",@"subTitle":@"Row1",@"avator":@"user_default_blue"},
           @{@"title":@"Section1",@"subTitle":@"Row2",@"avator":@"user_default_blue"}]};

 NSDictionary *dict2 = @{@"section":@"头部2",
       @"content":@[@{@"title":@"Section2",@"subTitle":@"Row0",@"avator":@"user_default_blue"},
           @{@"title":@"Section2",@"subTitle":@"Row1",@"avator":@"user_default_blue"},
           @{@"title":@"Section2",@"subTitle":@"Row2",@"avator":@"user_default_blue"}]};

 arrData = @[dict0,dict1,dict2];

 dicSelet = [[NSMutableDictionary alloc] init];

 //初始化选中状态(默认都不选择)
 for (NSInteger i=0; i<arrData.count; i++) {
  NSArray *content = arrData[i][@"content"];
  NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
  for (NSInteger j=0; j<content.count; j++) {
   [dict setObject:@"0" forKey:STR_NUM(j)];
  }
  [arrStatus addObject:dict];
 }

 //初始化列表头部折叠状态
 for (NSInteger i=0; i<arrData.count; i++) {
  [dictShow setObject:@"0" forKey:STR_NUM(i)];
 }
}

#pragma mark - TableViewDataSource,UITableViewDelegate 扩展

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

- (NSInteger)tableViewEx:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
 NSString *isShow = dictShow[STR_NUM(section)];
 if ([isShow isEqualToString:@"0"]) {
  NSArray *arr = arrData[section][@"content"];
  return arr.count;
 } else {
  return 0;
 }
}

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

- (UITableViewCell *)tableViewEx:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 static NSString * identifier = @"cellIdentifier";
 TableViewCell_TableSelect *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
 cell.selectionStyle = UITableViewCellSelectionStyleNone;
 if (cell == nil) {
  cell = [[TableViewCell_TableSelect alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
 }
 [cell setDictInfo:arrData[indexPath.section][@"content"][indexPath.row]];
 [cell setAccessoryImage:arrStatus[indexPath.section][STR_NUM(indexPath.row)]];

 return cell;
}

- (void)tableViewEx:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
 NSMutableDictionary *dict = arrStatus[indexPath.section];
 NSString *str = dict[STR_NUM(indexPath.row)];
 if ([str isEqualToString:@"0"]) {
  [dict setValue:@"1" forKey:STR_NUM(indexPath.row)];
 } else {
  [dict setValue:@"0" forKey:STR_NUM(indexPath.row)];
 }
 [self.tableView reloadData];
}

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

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

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

 UIView *headerView = [UICommonCtrl commonViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50) color:kColor_White];

 UILabel *title = [UICommonCtrl commonLabelWithFrame:CGRectMake(10, 15, 200, 20)
             text:arrData[section][@"section"]
             color:kColor_Black
             font:kFont_Large
           textAlignment:NSTextAlignmentLeft];
 [headerView addSubview:title];

 _imgArror = [UICommonCtrl commonImageViewWithFrame:CGRectMake(SCREEN_WIDTH-20, 22.5, 10, 5) image:nil];
 [headerView addSubview:_imgArror];

 NSString *str = [dictShow objectForKey:STR_NUM(section)];
 if ([str isEqualToString:@"0"]) {
  _imgArror.image = [UIImage imageNamed:@"icon_down"];
 } else {
  _imgArror.image = [UIImage imageNamed:@"icon_up"];
 }

 @weakify(self)
 UIButton *btn = [UICommonCtrl commonButtonWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)
             text:@""
             color:kColor_Black
             font:kFont_Large
          backgroundImage:nil
             block:^(UIButton *btn) {
              @strongify(self)
              NSString *str = [dictShow objectForKey:STR_NUM(section)];
              if ([str isEqualToString:@"0"]) {
               [dictShow setValue:@"1" forKey:STR_NUM(section)];
              } else {
               [dictShow setValue:@"0" forKey:STR_NUM(section)];
              }
              [self refreshSection:section];

             }];
 [headerView addSubview:btn];


 for (NSInteger i=0; i<2; i++) {
  UIView *line = [UICommonCtrl commonLineViewWithFrame:CGRectMake(0, (50-LINE_SIZE)*i, SCREEN_WIDTH, LINE_SIZE) color:kColor_Line];
  [headerView addSubview:line];
 }

 return headerView;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
 UIView *footerView = [UICommonCtrl commonViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10) color:kColor_Background];
 return footerView;
}

- (void)refreshSection:(NSInteger)section
{
 NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:section];
 [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];
}

@end

效果图

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • iOS多语言本地化流程的优化方案

    iOS多语言本地化流程的优化方案

    这篇文章主要给大家介绍了关于iOS多语言本地化流程的优化的相关资料,多语言本地化是我们大家在开发中经常会遇到的一个功能,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起看看吧。
    2018-01-01
  • Objective-C中字符串的拼接方法小结

    Objective-C中字符串的拼接方法小结

    这篇文章主要介绍了Objective-C中字符串的拼接方法小结,除了依靠NSString,文中还介绍了在宏里拼接字符串的方法,需要的朋友可以参考下
    2016-02-02
  • IOS中实现图片点击全屏预览

    IOS中实现图片点击全屏预览

    IOS作为一款智能手机系统,在查看图片的时候,如果能够实现全屏,对用户来说有很好的视觉体验,其实实现起来非常的简单,下面我就结合一个简单的代码给大家来分享一下,,需要的朋友可以参考下
    2015-11-11
  • iOS中解决Xcode9的Log日志无法输出中文的问题小结

    iOS中解决Xcode9的Log日志无法输出中文的问题小结

    这篇文章主要介绍了iOS中解决Xcode9的Log日志无法输出中文的问题小结,需要的朋友可以参考下
    2017-11-11
  • 深入解析iOS应用开发中对设计模式中的桥接模式的使用

    深入解析iOS应用开发中对设计模式中的桥接模式的使用

    这篇文章主要介绍了iOS应用开发中对设计模式中的桥接模式的使用,bridge桥接模式中主张把抽象部分与实现部分分离,需要的朋友可以参考下
    2016-03-03
  • iOS tableView右侧索引视图状态获取的方法实例

    iOS tableView右侧索引视图状态获取的方法实例

    tableView用于显示一个垂直滚动的单元格数(通常为可重复使用的单元格)组成的视图,这篇文章主要给大家介绍了关于iOS tableView右侧索引视图状态获取的相关资料,需要的朋友可以参考下
    2021-07-07
  • iOS实现图片保存与搜索功能

    iOS实现图片保存与搜索功能

    这篇文章主要介绍了iOS实现图片保存与搜索功能的相关资料,需要的朋友可以参考下
    2016-02-02
  • 详解IOS点击空白处隐藏键盘的几种方法介绍

    详解IOS点击空白处隐藏键盘的几种方法介绍

    本篇文章主要介绍了IOS点击空白处隐藏键盘的几种方法,非常具有实用价值,需要的朋友可以参考下。
    2016-12-12
  • iOS动画教你编写Slack的Loading动画进阶篇

    iOS动画教你编写Slack的Loading动画进阶篇

    这篇文章主要为大家进一步详细介绍了iOS动画教你编写Slack的Loading动画,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-09-09
  • iOS实现应用悬浮窗效果

    iOS实现应用悬浮窗效果

    这篇文章主要为大家详细介绍了iOS实现应用悬浮窗效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09

最新评论