歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> iOS 二級菜單(UITableView實現)

iOS 二級菜單(UITableView實現)

日期:2017/3/1 9:40:37   编辑:Linux編程

作為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://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /2014年資料/8月/25日/iOS 二級菜單(UITableView實現)

下載方法見 http://www.linuxidc.com/Linux/2013-07/87684.htm

------------------------------------------分割線------------------------------------------

Copyright © Linux教程網 All Rights Reserved