码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 二级菜单(UITableView实现)

时间:2014-07-28 16:22:33      阅读:351      评论:0      收藏:0      [点我收藏+]

标签:ios 二级菜单

作为iOS 新手 这个东西我捣鼓了一天,主要是没耐心。静下心来其实一会就能摆平。

我总结的经验,宁可精心学一个小时,也别浮躁学1天。

对新手来说主要是各种函数不熟,查询还不好查;

二级菜单网上说得不多,wo

下面来说一下这个二级菜单;

需求是这样的:

1 菜单只有二级。

2 如果有子菜单点一下打开,如果没有,则实现相应的操作;

我们来实现他(界面有点丑,但主要是功能,界面很简单自己设计一下就行):

个人想法是这样的:

首先建立一个cell的类,用于存放cell中的内容 ,继承自uitableviewcell;

TableCell.h

#import <UIKit/UIKit.h>
//tablecell的类
@interface TableCell : UITableViewCell
@property (nonatomic,retain) UILabel * Name;
@property (nonatomic,retain) UILabel * Comments;
@property (nonatomic,strong) NSArray *ChildArray;//存放子菜单
@property (nonatomic,assign) BOOL  Open;//表示子菜单是否打开
@end

TableCell.m

#import "TableCell.h"

@implementation TableCell
-(id)init
{
    if(self = [super init])
    {
        _Name = [[UILabel alloc] init];
        _Name.frame= CGRectMake(0, 0, 50, 30);
        [self.contentView addSubview:_Name];//将控件插入uitablviewecell
        _Comments = [[UILabel alloc]init];
        _Comments.frame = CGRectMake(60, 0, 50, 30);
        [self.contentView addSubview:_Comments];//将控件插入uitablviewecell
        _Open=false;//默认子控件是关闭的
    }
    return self;
}

@end

在.storyboard 中拖一个uiviewtable的控件;并且与设置属性  就是下面的TableView 并建立关联


或许我只是贴出代码来并不那么容易理解;

下面我说一下大体的思路吧;

 当选中cell的时候看看这个cell有没有子菜单,如果没有很简单直接打开就行了;

如果有那么我们先将这些子菜单想办法添加到掌管父菜单的数组中,然后生成一个位置数组(为了在tableview中调用

insertRowsAtIndexPaths:  withRowAnimation:

这个函数进行插入操作并且带有动画);

删除操作相同的意思先从控制父菜单的数组中删除,然后同样生成位置数组调用函数删除;

大体就是这样;主要是这两个函数来操作:

-(NSArray *) insertOperation:(TableCell *)item;//插入视图处理函数
-(NSArray *) deleteOperation:(TableCell *) item;//删除视图处理函数
好了来写:

工程中没有其他的类了,下面就是自动建好的.......Controller.h了

#import <UIKit/UIKit.h>
@class TableCell;
@interface TPViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>//实现uitableview的两个代理
@property (weak, nonatomic) IBOutlet UITableView *TableView;//UItableiew与.storyboard中拖的uitableview关联
@property (nonatomic,strong) NSMutableArray * TableArry;//要添加的进uitableview的数组,里面存放的是tablecell
@property (nonatomic,strong) NSMutableArray * InsertArry;//中间处理过程数组,用于插入子视图
@property (nonatomic,strong) NSMutableArray * DeleteArry;//中间处理过程数组,用于删除子视图
-(NSArray *) insertOperation:(TableCell *)item;//插入视图处理函数
-(NSArray *) deleteOperation:(TableCell *) item;//删除视图处理函数
@end

.m文件;

#import "TPViewController.h"
#import "TableCell.h"
#import "TableCellArray.h"
@interface TPViewController ()

@end

@implementation TPViewController

- (void)viewDidLoad
{
    
    [super viewDidLoad];
    _TableArry = [[NSMutableArray alloc]init];
    TableCell *cell0 = [[TableCell alloc]init];
    cell0.Name.text = @"子菜单";
    cell0.Comments.text = @"子菜单";
    cell0.ChildArray=nil;
    NSMutableArray *array = [[NSMutableArray alloc] init];
    [array addObject:cell0];
    TableCell *cell = [[TableCell alloc]init];
    cell.Name.text=@"菜单1";
    cell.Comments.text = @"注释1";
    cell.ChildArray = nil;
    [_TableArry addObject:cell];
    TableCell *cell1 = [[TableCell alloc]init];
    cell1.Name.text =@"菜单2";
    cell1.Comments.text = @"注释2";
    cell1.ChildArray = array;
    [_TableArry addObject:cell1];
    
    
    //以上是模拟数据
    
    
    _TableView.delegate=self;
    _TableView.dataSource=self;
    _InsertArry = [[NSMutableArray alloc]init];
    _DeleteArry = [[NSMutableArray alloc]init];
    
 	// Do any additional setup after loading the view, typically from a nib.
}
//返回tableview中cell的个数
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _TableArry.count;
}
//设置 cell的样式
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[TableCell alloc]init];
    if(indexPath.row<_TableArry.count)
    {
        cell = [_TableArry objectAtIndex:indexPath.row ];
    }
   
    return  cell;
}
//返回cell的高度
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 30.0f;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    
    // Dispose of any resources that can be recreated.
    
}
//当cell被选择(被点击)时调用的函数
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TableCell *cell=[_TableArry objectAtIndex:indexPath.row];
    NSLog(@"%d",indexPath.row);
    if(cell.ChildArray.count==0)//如果没有子菜单
    {
        NSLog(@"要打开页面");
    }
    else
    {
        if(!cell.Open)//如果子菜单是关闭的
        {
            NSArray * array =  [self insertOperation:cell];
            if(array.count>0)
                //从视图中添加
            [self.TableView insertRowsAtIndexPaths: array withRowAnimation:UITableViewRowAnimationBottom ];
            
        }
        else//如果子菜单是打开的
        {
            NSArray * array = [self deleteOperation:cell];
            if(array.count>0)
                //从视图中删除
            [self.TableView deleteRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationBottom];
        }
    }
}
-(NSArray *) insertOperation:(TableCell *)item
{
    [_InsertArry removeAllObjects];//将插入菜单清空
    NSIndexPath *path = [NSIndexPath indexPathForRow:[_TableArry indexOfObject:item] inSection:0];//获取选取的cell的位置
    NSLog(@"长度为%d",path.row);
    TableCell  *child = [[TableCell alloc]init];
    //遍历当前选取cell 的子菜单
    for(int i=0;i<item.ChildArray.count;i++)
    {
        child = [item.ChildArray objectAtIndex:i];
        [_TableArry insertObject:child atIndex:path.row + i +1 ];//调用数组函数将其插入其中
        [_InsertArry addObject:child];//放入插入数组中
    }
    item.Open=YES;//设置菜单已经打开
    NSMutableArray *PathArray= [NSMutableArray array];//初始化用于存放位置的数组
    for(TableCell * cell in _InsertArry)
    {
        NSIndexPath *path = [NSIndexPath indexPathForRow:[_TableArry indexOfObject:cell] inSection:0];
        [PathArray addObject:path];
    }
    return PathArray;
}
-(NSArray *) deleteOperation:(TableCell *)item
{
    [_DeleteArry removeAllObjects];//清空删除数组
    TableCell *child =[[TableCell alloc]init];//子菜单
    for(int i =0;i<item.ChildArray.count;i++)
    {
        child = [item.ChildArray objectAtIndex:i];
        [_DeleteArry addObject:child];//添加到删除数组
    }
    item.Open = NO;//设置子视图关闭
    NSMutableArray *mutableArry = [NSMutableArray array];
    NSMutableIndexSet *set= [NSMutableIndexSet indexSet];//设置用来存放删除的cell的索引
    for(TableCell *cell in _DeleteArry)
    {
        NSIndexPath *path = [NSIndexPath indexPathForRow:[_TableArry indexOfObject:cell] inSection:0];
        NSLog(@"%d",path.row);
        [mutableArry addObject:path];
        [set addIndex:path.row];
    }
    [_TableArry removeObjectsAtIndexes:set];//调用函数来从数组中删除
    return mutableArry;
}
@end


这个主要是参考csdn上下载的一个二级菜单的例子;

但是有些不一样,如果他的代码你看不懂,把我的看懂了再去看他的就简单了;

可以下载我的源码运行看一下;http://download.csdn.net/detail/u010123208/7685367

iOS 二级菜单(UITableView实现),布布扣,bubuko.com

iOS 二级菜单(UITableView实现)

标签:ios 二级菜单

原文地址:http://blog.csdn.net/u010123208/article/details/38176943

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!