溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在iOS中實(shí)現(xiàn)一個(gè)列表折疊效果

發(fā)布時(shí)間:2021-04-16 17:43:06 來(lái)源:億速云 閱讀:354 作者:Leah 欄目:移動(dòng)開(kāi)發(fā)

這篇文章給大家介紹怎么在iOS中實(shí)現(xiàn)一個(gè)列表折疊效果,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

實(shí)現(xiàn)列表折疊效果其實(shí)比較簡(jiǎn)單,點(diǎn)擊列表頭部的時(shí)候,把返回列表行數(shù)設(shè)為 0,就是收起列表;再次點(diǎn)擊列表頭部,顯示列表的行數(shù),就展開(kāi)了列表。

#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];

 //初始化選中狀態(tài)(默認(rèn)都不選擇)
 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];
 }

 //初始化列表頭部折疊狀態(tài)
 for (NSInteger i=0; i<arrData.count; i++) {
  [dictShow setObject:@"0" forKey:STR_NUM(i)];
 }
}

#pragma mark - TableViewDataSource,UITableViewDelegate 擴(kuò)展

- (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

關(guān)于怎么在iOS中實(shí)現(xiàn)一個(gè)列表折疊效果就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

ios
AI