这篇文章给大家介绍怎么在iOS中实现一个列表折叠效果,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
创新互联长期为近千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为中阳企业提供专业的成都做网站、网站制作,中阳网站改版等技术服务。拥有十载丰富建站经验和众多成功案例,为您定制开发。实现列表折叠效果其实比较简单,点击列表头部的时候,把返回列表行数设为 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中实现一个列表折叠效果就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
新闻标题:怎么在iOS中实现一个列表折叠效果-创新互联
文章位置:https://www.cdcxhl.com/article4/dccsie.html
成都网站建设公司_创新互联,为您提供响应式网站、云服务器、网站设计公司、定制开发、动态网站、搜索引擎优化
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联