分享

使用TableView实现多级树型menu

 永恒clek4xeu0r 2017-01-15
官方UIKit下的TableView,支持section和row的显示,但不支持在talbeview里显示多级树型结构的menu,因为项目需要便写了一个支持多级目录显示menu的Demo(下载传送门)。支持菜单展开动画效果,支持级联打开下下级子目录。

效果图如下:


要现实多级目录,首先要做的是在内存构建树型结构,通过这个树型结构,当用户点击了某个有子项的菜单,其变会根据树型结构将menu展开或收起。

下面是“树”中节点的构造,在这里并没有使用CFTree,出于两点考虑:一是menu的结构一般只需知道其子菜单即可,是一个简单应用;第二是项目内部对C 不了的童鞋。

  1. @interface MyItem : NSObject  
  2. @property (nonatomic,retain)  NSString * title;  
  3. @property (nonatomic) NSInteger level;  
  4. @property (nonatomic, retain) NSMutableArray *subItems;  
  5. @property (nonatomic) BOOL isSubItemOpen;  
  6. @property (nonatomic) BOOL isCascadeOpen;  
  7. @end  
其中,subItems这个数组,存放该节点的子菜单,使用isSubItemOpen来标记自菜单是否被打开,使用isCascadeOpen标记该子菜单是否要在其父菜单展开时自动展开。

菜单级联收起代码

  1. - (NSMutableArray *) cascadingDeletePaths:(NSIndexPath *)indexPath  
  2. {  
  3.     [treeItemsToRemove removeAllObjects];  
  4.     MyItem * item;  
  5.     item = [_tableViewData objectAtIndex:indexPath.row];  
  6.       
  7.     [self cascadingDeleteCellPaths:item];  
  8.       
  9.     NSMutableIndexSet * set;  
  10.     set = [NSMutableIndexSet indexSet];  
  11.     for (int i = 0; i < [treeItemsToRemove count]; i ) {  
  12.         NSIndexPath *index;  
  13.         index  = [treeItemsToRemove objectAtIndex:i];  
  14.         [set addIndex:index.row];  
  15.     }  
  16.     item.isSubItemOpen = NO;  
  17.     [_tableViewData removeObjectsAtIndexes:set];  
  18.     return treeItemsToRemove;      
  19. }  
  20. - (void) cascadingDeleteCellPaths:(MyItem *)item  
  21. {  
  22.       
  23.     for (int i = 0; i < [item.subItems count] && item.isSubItemOpen ; i ) {  
  24.         MyItem * item1;  
  25.         item1 = [item.subItems objectAtIndex:i];  
  26.         NSLog(@'sub %@',item1);  
  27.   
  28.         NSIndexPath *path = [NSIndexPath indexPathForRow:[_tableViewData indexOfObject:item1] inSection:0];  
  29.           
  30.         [self cascadingDeleteCellPaths:item1];  
  31.         [treeItemsToRemove addObject:path];  
  32.         item1.isSubItemOpen = NO;  
  33.     }  
  34. }  
其中使用cascadingDeleteCellPaths函数的递归调用,来完成对item子树的遍历。


在tableViewController里逻辑要尽量简单,其中在tableView:didSelectRowAtIndexPath里代码如下

  1. MenuItemCell * cell;  
  2. cell = (MenuItemCell *)[tableView cellForRowAtIndexPath:indexPath];  
  3. if (cell.item.isSubItemOpen)  
  4. {  
  5.     //remove  
  6.     NSArray * arr;  
  7.     arr = [_menuData cascadingDeletePaths:indexPath];  
  8.     if ([arr count] >0) {  
  9.         [tableView deleteRowsAtIndexPaths: arr withRowAnimation:UITableViewRowAnimationBottom];  
  10.     }  
  11. }  
  12. else  
  13. {  
  14.     //insert  
  15.     NSArray * arr;  
  16.     arr = [_menuData cascadingInsertCellPaths:indexPath];  
  17.     if ([arr count] >0) {  
  18.         [tableView insertRowsAtIndexPaths:arr withRowAnimation:UITableViewRowAnimationBottom];  
  19.     }  
  20. }  

其中,cell的插入和移除的动画,使用withRowAnimation完成。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多