使用TableView实现多级树型menu

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

效果图如下:


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

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

[cpp]  view plain copy
  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标记该子菜单是否要在其父菜单展开时自动展开。

菜单级联收起代码

[cpp]  view plain copy
  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里代码如下

[cpp]  view plain copy
  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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你可以使用QTableView实现翻页功能。下面是一个简单的示例代码: ```cpp #include <QApplication> #include <QTableView> #include <QStandardItemModel> #include <QPushButton> #include <QVBoxLayout> int main(int argc, char *argv[]) { QApplication app(argc, argv); // 创建数据模型 QStandardItemModel model; model.setColumnCount(1); model.setRowCount(100); // 假设有100条数据 // 创建表格视图 QTableView tableView; tableView.setModel(&model); // 设置每页显示的行数 int rowsPerPage = 10; // 创建翻页按钮 QPushButton previousButton("Previous"); QPushButton nextButton("Next"); // 创建布局管理器 QVBoxLayout layout; // 将表格视图和按钮添加到布局中 layout.addWidget(&tableView); layout.addWidget(&previousButton); layout.addWidget(&nextButton); // 设置布局管理器 QWidget window; window.setLayout(&layout); // 当前页数 int currentPage = 0; // 点击上一页按钮时的响应 QObject::connect(&previousButton, &QPushButton::clicked, [&]() { if (currentPage > 0) { currentPage--; tableView.scrollTo(model.index(currentPage * rowsPerPage, 0)); } }); // 点击下一页按钮时的响应 QObject::connect(&nextButton, &QPushButton::clicked, [&]() { if (currentPage < model.rowCount() / rowsPerPage) { currentPage++; tableView.scrollTo(model.index(currentPage * rowsPerPage, 0)); } }); window.show(); return app.exec(); } ``` 这个示例中,通过点击"Previous"和"Next"按钮可以切换表格视图中的页数。每页显示的行数可以通过`rowsPerPage`变量设置。你可以根据自己的需要修改代码。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值