身為 iOS 新手,這個東西我折騰了一整天,主要問題是沒耐心。靜下心來其實一會兒就能搞定。我的體會是:寧可專心學一小時,也不要浮躁學一整天。
新手的主要障礙是各種函式不熟、又不好查。二級菜單的教學網上不多,所以把過程記錄下來。
界面有點樸素,重點是功能,你可以自行美化。
先建一個 cell 類別用來承載內容,繼承自 UITableViewCell;再讓每個 cell 自己記住有沒有子菜單、以及子菜單是否已展開。
當 cell 被選取時,先看它有沒有子菜單:沒有就直接處理;有的話,先把子項加入掌管父菜單的陣列,同時算出位置陣列,交給 insertRowsAtIndexPaths:withRowAnimation: 插入並帶動畫。刪除同理:先從陣列移除,再算出位置陣列呼叫刪除。核心就是這兩個函式:
-(NSArray *) insertOperation:(TableCell *)item; // 插入視圖處理函式
-(NSArray *) deleteOperation:(TableCell *)item; // 刪除視圖處理函式
#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
#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];
_Comments = [[UILabel alloc] init];
_Comments.frame = CGRectMake(60, 0, 50, 30);
[self.contentView addSubview:_Comments];
_Open = false; // 預設子菜單關閉
}
return self;
}
@end
接著在 storyboard 中拖一個 UITableView 進來,設定 delegate/dataSource 並與下面的 TableView 屬性建立關聯。
#import <UIKit/UIKit.h>
@class TableCell;
@interface TPViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *TableView; // 與 storyboard 關聯
@property (nonatomic, strong) NSMutableArray *TableArry; // 要放進 tableview 的陣列,元素為 TableCell
@property (nonatomic, strong) NSMutableArray *InsertArry; // 插入用的中間陣列
@property (nonatomic, strong) NSMutableArray *DeleteArry; // 刪除用的中間陣列
-(NSArray *) insertOperation:(TableCell *)item;
-(NSArray *) deleteOperation:(TableCell *)item;
@end
#import "TPViewController.h"
#import "TableCell.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];
}
// 回傳 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;
}
// 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 的位置
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 *p = [NSIndexPath indexPathForRow:[_TableArry indexOfObject:cell] inSection:0];
[PathArray addObject:p];
}
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]; // 存放要刪除的索引
for (TableCell *cell in _DeleteArry)
{
NSIndexPath *path = [NSIndexPath indexPathForRow:[_TableArry indexOfObject:cell] inSection:0];
[mutableArry addObject:path];
[set addIndex:path.row];
}
[_TableArry removeObjectsAtIndexes:set]; // 從陣列中移除
return mutableArry;
}
@end
這個實作參考了一份開源的二級菜單範例,但做了一些調整。如果別人的版本你看不懂,先把這份讀通再回頭看,會輕鬆很多。
源碼下載:
------------------------------------------分割線------------------------------------------
免費下載位址 https://linux.linuxidc.com/
帳號與密碼都是 www.linuxidc.com
下載目錄 /2014年資料/8月/25日/iOS 二級菜單(UITableView實現)
下載方法見 https://www.linuxidc.com/Linux/2013-07/87684.htm
------------------------------------------分割線------------------------------------------