展开和折叠所有表格视图单元格

巴金斯先生

我需要一些UI建议。

我有一个视图,需要加载这样的格式的数据:

    {
      "heading": "This is a header",
      "content": "This is some detailed content about the header"
    },
    {
      "heading": "This is another headline.",
      "content": " These are more details about the headline. "
    }

这里是参数:加载时,它应该仅在表格视图中显示标题。点击标题将扩展该单元格并加载其内容或详细信息。这是一个粗略的草图:

负载:

  • 标题1
  • 标题2
  • 标题3
  • 标题4

在标题2上:

  • 标题1
  • 标题2
    • 标题2的内容在此处显示
  • 标题3
  • 标题4

还需要有一个将扩展或折叠所有单元格的条形按钮项。哪个会这样:

全部收缩:

  • 标题1
  • 标题2
  • 标题3
  • 标题4

展开全部:

  • 标题1
    • 标题1的内容在此处显示
  • 标题2
    • 标题2的内容在此处显示
  • 标题3
    • 标题3的内容在此处显示
  • 标题4
    • 标题4的内容在此处显示

我使用了一些怪异的父/子逻辑来扩展单个单元格,但是我想我走了一条黑暗的路,因为现在我试图实现全部扩展/折叠,而我被困住了。

有谁知道做这种手风琴表格视图的任何开源代码和/或关于如何设置视图控制器来做到这一点的任何建议?我已经看到一些库可以扩展和折叠单个单元格,但是能够完成所有这些单元变得很棘手。

杰夫

这就是我的方法,也许稍微简单一些,尽管绝对是相似的方法:)

#import "ViewController.h"

//dont worry, the header is empty except for import <UIKit/UIKit.h>, this is a subclass on UIViewController

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) UITableView *tableView;


@end

@implementation ViewController
{
//ivars
  BOOL sectionIsOpen[4]; //we will use this BOOL array to keep track of the open/closed state for each section.  Obviously I have the number of sections fixed at 4 here, but you could make a more dynamic array with malloc() if neccesary..
}



- (void)viewDidLoad {
  [super viewDidLoad];

  UITableView *tv = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
  tv.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  tv.dataSource = self;
  tv.delegate = self;

  [self.view addSubview:tv];
  self.tableView = tv;

  // Do any additional setup after loading the view, typically from a nib.
}

#pragma mark - UITableViewDataSource
-(NSInteger )numberOfSectionsInTableView:(UITableView *)tableView{
  return 4;
}

-(NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  return ((sectionIsOpen[section]) ? [self numberOfRowsInSection:section] : 0);
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

  //put your switch() here...

  return [NSString stringWithFormat:@"I am section %i", (int)section ];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

  static NSString *cellId = @"cellID";

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];

  if (!cell) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
  }


 //etc etc decorate your cell...
  cell.textLabel.text = [NSString stringWithFormat:@"cell %i / %i", (int)indexPath.section, (int)indexPath.row ];


  return cell;
}
#pragma mark - UITableViewDelegate
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

  const CGRect fr = CGRectMake(0, 0, 320.0, 40.0 );

  UIButton *btn = [[UIButton alloc]initWithFrame:fr];
  [btn setTitle:[self tableView:tableView titleForHeaderInSection:section] forState:UIControlStateNormal ];
  [btn setTag:section];
  [btn addTarget:self action:@selector(sectionOpenToggle:) forControlEvents:UIControlEventTouchUpInside];


  // add an image, colour etc if you like

  return btn;
}



#pragma mark - tableViewHelpers

//the number of rows in sectionX when it is open...
-(NSInteger )numberOfRowsInSection:(NSInteger )section{

  return section + 1;
}

//opening/closing a section
-(void )setSection:(NSInteger )section toOpen:(BOOL )open{

  if (open != sectionIsOpen[section]) {

//build an array of indexPath objects
    NSMutableArray *indxPths = [NSMutableArray array];
    for (NSInteger i = 0; i < [self numberOfRowsInSection:section]; i ++) {

      [indxPths addObject: [NSIndexPath indexPathForRow:i inSection:section ]
       ];

    }


    [self.tableView beginUpdates];

    if (open) {
      [self.tableView insertRowsAtIndexPaths:indxPths withRowAnimation:UITableViewRowAnimationFade];
      //nb there is a large ENUM of tableViewRowAnimation types to experiment with..

    }else{
      [self.tableView deleteRowsAtIndexPaths:indxPths withRowAnimation:UITableViewRowAnimationFade];

    }
    sectionIsOpen[section] = open;
    [self.tableView endUpdates];

  }
}

-(void )sectionOpenToggle:(id )sender{
  [self setSection:[sender tag] toOpen: !sectionIsOpen[[sender tag]] ];
}

// open/close all sections.
-(void )setAllSectionsOpen:(BOOL )open{

  for (NSInteger i = 0; i < [self numberOfSectionsInTableView:self.tableView]; i ++) {
    [self setSection:i toOpen:open];
  }
}


//these two for your convenience, hook up to navbar items etc..
-(IBAction)openAllSections:(id)sender{
  [self setAllSectionsOpen:YES];
}
-(IBAction)closeAllSections:(id)sender{
  [self setAllSectionsOpen:NO];
}
@end

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

展开和折叠所有表格视图单元格

来自分类Dev

在iOS中展开和折叠表格视图单元格

来自分类Dev

在iOS中展开和折叠表格视图单元格

来自分类Dev

表格视图单元格展开和折叠时出现问题

来自分类Dev

具有两个标签的展开/折叠自调整大小的表格视图单元格

来自分类Dev

带有两个标签的展开/折叠自调整大小的表格视图单元格

来自分类Dev

展开和折叠tableview单元格

来自分类Dev

如何展开表格视图单元格?

来自分类Dev

如何展开表格视图单元格?

来自分类Dev

表格视图单元格未显示所有文本

来自分类Dev

如何使tableview单元格展开和折叠

来自分类Dev

展开UITableView以显示堆栈视图中的所有单元格吗?

来自分类Dev

展开/压缩 UICollectionView 中的所有 UICollectionView 单元格

来自分类Dev

Excel宏-选择具有数据和格式的所有单元格作为表格

来自分类Dev

具有大图像和标签的自定义表格视图单元格

来自分类Dev

表格视图中的单元格和行有什么区别?

来自分类Dev

表格视图,单元格编辑和拖动

来自分类Dev

填写表格视图部分和单元格

来自分类Dev

如何快速显示和隐藏表格视图单元格

来自分类Dev

表格视图,单元格编辑和拖动

来自分类Dev

通过使用约束自行调整大小来展开和折叠 TableView 单元格

来自分类Dev

带有2个部分的单元格的表格视图

来自分类Dev

表格视图中的单元格没有响应

来自分类Dev

致命错误:在展开表格视图单元格上的Optional值时意外发现nil

来自分类Dev

使用显示/减少按钮展开/折叠 tableview 单元格

来自分类Dev

如何整合表格中的所有td单元格

来自分类Dev

在谷歌表格中定位所有单元格的位置

来自分类Dev

表格单元格上的ng-change更新所有单元格

来自分类Dev

表格视图单元格中的按钮

Related 相关文章

  1. 1

    展开和折叠所有表格视图单元格

  2. 2

    在iOS中展开和折叠表格视图单元格

  3. 3

    在iOS中展开和折叠表格视图单元格

  4. 4

    表格视图单元格展开和折叠时出现问题

  5. 5

    具有两个标签的展开/折叠自调整大小的表格视图单元格

  6. 6

    带有两个标签的展开/折叠自调整大小的表格视图单元格

  7. 7

    展开和折叠tableview单元格

  8. 8

    如何展开表格视图单元格?

  9. 9

    如何展开表格视图单元格?

  10. 10

    表格视图单元格未显示所有文本

  11. 11

    如何使tableview单元格展开和折叠

  12. 12

    展开UITableView以显示堆栈视图中的所有单元格吗?

  13. 13

    展开/压缩 UICollectionView 中的所有 UICollectionView 单元格

  14. 14

    Excel宏-选择具有数据和格式的所有单元格作为表格

  15. 15

    具有大图像和标签的自定义表格视图单元格

  16. 16

    表格视图中的单元格和行有什么区别?

  17. 17

    表格视图,单元格编辑和拖动

  18. 18

    填写表格视图部分和单元格

  19. 19

    如何快速显示和隐藏表格视图单元格

  20. 20

    表格视图,单元格编辑和拖动

  21. 21

    通过使用约束自行调整大小来展开和折叠 TableView 单元格

  22. 22

    带有2个部分的单元格的表格视图

  23. 23

    表格视图中的单元格没有响应

  24. 24

    致命错误:在展开表格视图单元格上的Optional值时意外发现nil

  25. 25

    使用显示/减少按钮展开/折叠 tableview 单元格

  26. 26

    如何整合表格中的所有td单元格

  27. 27

    在谷歌表格中定位所有单元格的位置

  28. 28

    表格单元格上的ng-change更新所有单元格

  29. 29

    表格视图单元格中的按钮

热门标签

归档