iOS 二级菜单(UITableView实现)「建议收藏」

iOS 二级菜单(UITableView实现)「建议收藏」iOS二级菜单(UITableView实现)注释全帮助新手学习

大家好,又见面了,我是你们的朋友全栈君。

作为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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/133445.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • java8中 Collectors.groupingBy用法

    java8中 Collectors.groupingBy用法Collectors.groupingBy根据一个或多个属性对集合中的项目进行分组1、数据准备:publicProduct(Longid,Integernum,BigDecimalprice,Stringname,Stringcategory){this.id=id;this.num=num;this.price=price;this.name=name;this.category=category;}…

    2022年8月20日
  • 词向量的作用

    词向量的作用      为了描述词与词之间的相似程度而构建的高维向量,而视频,音频数据构建本身意义,可以直接通过数据进行描述图像与声音的相似度。

    2022年5月17日
  • JG指令_JZ指令

    JG指令_JZ指令逆向之旅001_攻防世界game写在前面攻防世界的第一题game第一步:运行这个exe使用IDA反编译总结写在前面这是我的第一篇博客,从大二开始接触网络安全的知识,现在已经大四了.回首过去,课外的实践主要是在跟着导师做态势感知的项目,从写爬虫到搭网站再到写定位算法再到去参加信安作品赛。。。。在这个过程中,我的正向开发能力确实提高了。但逆向作为网安人必不可少的能力,我之前没有花时间钻研过。目前掌握的关于逆向的基础知识都是在课堂上学到的,例如栈溢出,堆溢出,UAF,pe文件格式,代码保护技术,汇编语言,编译

    2022年10月27日
  • windows通过ssh登陆linux服务器(linux 终端快捷键)

    windows通过ssh登陆linux服务器(linux 终端快捷键)window通过ssh连接linux1.window上要安装ssh   下载连接:https://www.mls-software.com/opensshd.html   版本:OpenSSH7.9p1-1   下载好后安装2.linux上启动ssh服务   有些可能没有ssh服务,需要下载安装   2.1检查是否有ssh服务:   判断是否安装ssh服务,可以通过如…

    2022年4月18日
  • MessageDigest简单介绍

    MessageDigest简单介绍本文博客原文參考文章:http://blog.sina.com.cn/s/blog_4f36423201000c1e.html一、概述java.security.MessageDigest类用于为应用

    2022年6月30日
  • 在总线周期的t1,t2,t3,t4状态,cpu_计算机组成原理总线带宽怎么算

    在总线周期的t1,t2,t3,t4状态,cpu_计算机组成原理总线带宽怎么算大家好,我是小黄鸭,又来更新了,应小伙伴的需要,该实验也过了。实验所用的软件资源/测试电路也全部开放,地址在MOOC中国大学为:https://www.icourse163.org/learn/HUST-1205809816#/learn/announce附带实验测试,地址在Educode上为:https://www.educoder.net/shixuns/ckff6yv9/challenges光是给的Excel自生成电路表格就上了7个,再加上密密麻麻的电路图,各自安好吧整体框架该实验

    2022年10月13日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号