IOS Level 2 menu (UITableView implementation)
As a beginner in iOS, I have been busy for a day, mainly because I have no patience. Calm down, but it will take a while.
I would rather carefully study for an hour than just one day.
For beginners, it is mainly because various functions are not familiar and query is not easy to query;
I don't talk much about Level 2 menus online, wo
Next, let's take a look at this level-2 menu;
The requirement is as follows:
1. The menu has only two levels.
2. If you have a sub-menu, click it to open it. If not, perform the corresponding operation;
Let's implement it (the interface is a bit ugly, but the main function is that the interface is simple and can be designed by yourself ):
My personal thoughts are as follows:
First, create a cell class to store the content in the cell and inherit from uitableviewcell;
TableCell. h
# Import
// Tablecell class @ interface TableCell: UITableViewCell @ property (nonatomic, retain) UILabel * Name; @ property (nonatomic, retain) UILabel * Comments; @ property (nonatomic, strong) NSArray * ChildArray; // stores the sub-menu @ property (nonatomic, assign) BOOL Open; // indicates whether the Sub-menu is opened @ 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]; // Insert the control into uitablviewecell _ Comments = [[UILabel alloc] init]; _ Comments. frame = CGRectMake (60, 0, 50, 30); [self. contentView addSubview: _ Comments]; // Insert the control into uitablviewecell _ Open = false; // The default sub-control is disabled} return self;} @ end
Drag a uiviewtable control in. storyboard, and associate it with the TableView attribute below.
Maybe I just paste the code and it's not that easy to understand;
Let me talk about the general idea below;
When the cell is selected, check whether there are sub-menus for the cell. If not, simply open the cell directly;
If so, we first add these sub-menus to the array in charge of the parent menu, and then generate a location array (to call
insertRowsAtIndexPaths: withRowAnimation:
This function is inserted and animated );
In the same way, the delete operation first deletes the array from the control parent menu, and then calls the function to delete the location array;
This is basically the case. These two functions are mainly used for operations:
-(NSArray *) insertOperation :( TableCell *) item; // insert View Handler-(NSArray *) deleteOperation :( TableCell *) item; // Delete view Handler
Let's write:
There are no other classes in the project. The following is the automatically created class... Controller. h.
# Import
@ Class TableCell; @ interface TPViewController: UIViewController
// Implement two uitableview proxies @ property (weak, nonatomic) IBOutlet UITableView * TableView; // UItableiew and. the uitableview Association dragged in the storyboard @ property (nonatomic, strong) NSMutableArray * TableArry; // the array of the uitableview to be added, which stores tablecell @ property (nonatomic, strong) NSMutableArray * InsertArry; // an array of intermediate processing processes, used to insert a subview @ property (nonatomic, strong) NSMutableArray * DeleteArry; // an array of intermediate processing processes, used to delete a subview-(NSArray *) insertOperation :( TableCell *) item; // insert a view Handler-(NSArray *) deleteOperation :( TableCell *) item; // Delete the view handler @ end
. M file;
# 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 = @ "sub-menu"; cell0.Comments. text = @ "sub-menu"; cell0.ChildArray = nil; NSMutableArray * array = [NSMutableArray alloc] init]; [Array addObject: cell0]; TableCell * cell = [[TableCell alloc] init]; cell. name. text = @ "menu 1"; cell. comments. text = @ "comment 1"; cell. childArray = nil; [_ TableArry addObject: cell]; TableCell * cell1 = [[TableCell alloc] init]; cell1.Name. text = @ "menu 2"; cell1.Comments. text = @ "comment 2"; cell1.ChildArray = array; [_ TableArry addObject: cell1]; // The above is the simulated data _ TableView. delegate = self; _ TableView. dataSource = self; _ Insert Arry = [[NSMutableArray alloc] init]; _ DeleteArry = [[NSMutableArray alloc] init]; // Do any additional setup after loading the view, typically from a nib .} // return the number of cells in tableview-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {return _ TableArry. count;} // set the cell style-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPa Th {UITableViewCell * cell = [[TableCell alloc] init]; if (indexPath. row <_ TableArry. count) {cell = [_ TableArry objectAtIndex: indexPath. row];} return cell;} // return cell height-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath {return 30366f;}-(void) didReceiveMemoryWarning {[super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated .} // when cel L function called when selected (clicked)-(void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {TableCell * cell = [_ TableArry objectAtIndex: indexPath. row]; NSLog (@ "% d", indexPath. row); if (cell. childArray. count = 0) // if no sub-menu exists {NSLog (@ "open page");} else {if (! Cell. open) // if the sub-menu is closed {NSArray * array = [self insertOperation: cell]; if (array. count> 0) // Add [self. tableView insertRowsAtIndexPaths: array withRowAnimation: UITableViewRowAnimationBottom];} else // if the sub-menu is open {NSArray * array = [self deleteOperation: cell]; if (array. count> 0) // Delete [self. tableView deleteRowsAtIndexPaths: array withRowAnimation: UITableViewRowAnimationBottom] ;}}- (NSArray *) insertOperation :( TableCell *) item {[_ InsertArry removeAllObjects]; // clear the Insert menu NSIndexPath * path = [NSIndexPath indexPathForRow: [_ TableArry indexOfObject: item] inSection: 0]; // obtain the NSLog (@ "Length: % d", path. row); TableCell * child = [[TableCell alloc] init]; // traverses the sub-menu of the currently selected cell for (int I = 0; I
This is mainly an example of a level-2 menu downloaded from csdn;
But there are some differences. If you don't understand his code, it's easy to read it again;
Can download my source code run a look; http://download.csdn.net/detail/u010123208/7685367