iOS 二級菜單(UITableView實現)
作為iOS 新手 這個東西我搗鼓了一天,主要是沒耐心。靜下心來其實一會就能擺平。
我總結的經驗,寧可精心學一個小時,也別浮躁學1天。
對新手來說主要是各種函數不熟,查詢還不好查;
二級菜單網上說得不多,wo
下面來說一下這個二級菜單;
需求是這樣的:
1 菜單只有二級。
2 如果有子功能表點一下開啟,如果沒有,則實現相應的操作;
我們來實現他(介面有點醜,但主要是功能,介面很簡單自己設計一下就行):
個人想法是這樣的:
首先建立一個cell的類,用於存放cell中的內容 ,繼承自uitableviewcell;
TableCell.h
#import //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 @class TableCell;@interface TPViewController : UIViewController//實現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
這個主要是參考csdn上下載的一個二級菜單的例子;
但是有些不一樣,如果他的代碼你看不懂,把我的看懂了再去看他的就簡單了;
可以下載我的源碼運行看一下;http://download.csdn.net/detail/u010123208/7685367