IOS Level Two menu (UITableView implementation)

Source: Internet
Author: User

As an iOS novice this thing I have been tinkering with the day, mainly impatient. Calm down and you'll be able to fix it in a minute.

I have summed up the experience, rather careful study one hours, also do not impetuous study 1 days.

For the novice is mainly a variety of functions are not ripe, query is not good to check;

Second-level menu is not much to say, Wo

Here's a two-level menu;

The requirement is this:

The 1 menu is only two levels.

2 If there is a sub-menu click Open, if not, then implement the corresponding operation;

We come to realize him (the interface is a bit ugly, but the main function, the interface is very simple to design on the line):

The personal idea is this:

First, a cell class is established for storing the contents of the cell and inheriting from UITableViewCell;

TableCell.h

Class of the #import <uikit/uikit.h>//tablecell @interface tablecell:uitableviewcell@property (nonatomic,retain) UILabel * NAME; @property (nonatomic,retain) UILabel * Comments; @property (nonatomic,strong) Nsarray *childarray;// Store submenu @property (nonatomic,assign) BOOL  open;//Indicates whether the submenu is open @end

tablecell.m

#import "TableCell.h" @implementation tablecell-(ID) init{    if (self = [super init])    {        _name = [[UILabel alloc ] init];        _name.frame= cgrectmake (0, 0, +);        [Self.contentview addsubview:_name];//inserts the control into Uitablviewecell        _comments = [[UILabel alloc]init];        _comments.frame = CGRectMake (0, +);        [Self.contentview addsubview:_comments];//Insert control into Uitablviewecell        _open=false;//default child control is off    }    return Self;} @end

Drag a Uiviewtable control in. Storyboard, and set the property to be the following TableView and establish the association


Maybe I just posted the code to not be so easy to understand;

Let me say a general idea.

When the cell is selected to see if this cell has a submenu, if it is not easy to open directly on the line;

If so, let's add these submenus to the array that governs the parent menu, and then generate an array of positions (in order to invoke the TableView in the

Insertrowsatindexpaths:  withrowanimation:

This function is inserted and animated);

The same meaning of the delete operation is removed from the array that controls the parent menu, and then the same generated position array is called to delete the function;

This is basically the case: the two functions are essentially the same.

-(Nsarray *) Insertoperation: (TableCell *) item;//Insert View handler function-(Nsarray *) Deleteoperation: (TableCell *) item;//Delete View handler function
All right, here's the writing:

There is no other class in the project, the following is automatically built .... Controller.h.

#import <UIKit/UIKit.h> @class TableCell; @interface tpviewcontroller:uiviewcontroller<uitableviewdelegate , uitableviewdatasource>//implements the UITableView two agent @property (weak, nonatomic) Iboutlet UITableView *TableView;// Uitableiew UITableView associated with. Storyboard @property (nonatomic,strong) Nsmutablearray * tablearry;// An array of UITableView to be added, containing an array of Tablecell@property (Nonatomic,strong) Nsmutablearray * insertarry;//Intermediate processes, Used to insert a sub-view @property (Nonatomic,strong) Nsmutablearray * deletearry;//An array of intermediate processing procedures for deleting a child view-(Nsarray *) Insertoperation: ( TableCell *) item;//Insert View handler function-(Nsarray *) Deleteoperation: (TableCell *) item;//Delete 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];    [email protected] "Menu 1";    Cell.Comments.text = @ "Note 1"; Cell.    Childarray = nil;    [_tablearry Addobject:cell];    TableCell *cell1 = [[TableCell alloc]init]; Cell1.    Name.text [email protected] "Menu 2";    Cell1.Comments.text = @ "NOTE 2"; Cell1.    Childarray = array;            [_tablearry ADDOBJECT:CELL1];    The above is the analog data _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.}    Returns the number of cells in the TableView-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (Nsinteger) section{ return _tablearry.count;}    Set the cell's style-(uitableviewcell*) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{    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 30.0f;}        -(void) didreceivememorywarning{[Super didreceivememorywarning];    Dispose of any resources the can be recreated.    }//function called when cell is selected (void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{    TableCell *cell=[_tablearry ObjectAtIndex:indexPath.row];    NSLog (@ "%d", indexpath.row); if (cell. childarray.count==0)//If there is no submenu {NSLog (@ "To open the page"); } else {if (!cell.            Open)//If the submenu is closed {Nsarray * array = [self Insertoperation:cell]; if (array.count>0)//Add from view [self.                    TableView Insertrowsatindexpaths:array Withrowanimation:uitableviewrowanimationbottom];            } else//if the submenu is open {Nsarray * array = [self Deleteoperation:cell]; if (array.count>0)//Remove from view [self.        TableView Deleterowsatindexpaths:array Withrowanimation:uitableviewrowanimationbottom]; }}}-(Nsarray *) Insertoperation: (TableCell *) item{[_insertarry removeallobjects];//Insert menu empty Nsindexpath *path =    [Nsindexpath Indexpathforrow:[_tablearry Indexofobject:item] insection:0];//Gets the location of the selected cell NSLog (@ "Length%d", path.row);    TableCell *child = [[TableCell alloc]init]; Iterates through the currently selected cell's submenu for (int i=0;i<item. childarray.count;i++) {child = [item]. Childarray ObjectatINDEX:I];    [_tablearry insertobject:child AtIndex:path.row + i +1];//call an array function to insert it [_insertarry addobject:child];//into the insert array } item. open=yes;//Settings menu is already open Nsmutablearray *patharray= [nsmutablearray array];//initialize array for storage location for (TableCell * cell in _insert        Arry) {Nsindexpath *path = [Nsindexpath indexpathforrow:[_tablearry Indexofobject:cell] inSection:0];    [Patharray Addobject:path]; } return Patharray;} -(Nsarray *) Deleteoperation: (TableCell *) item{[_deletearry removeallobjects];//empty Delete array TableCell *child =[[tablecel L alloc]init];//submenu for (int i =0;i<item. childarray.count;i++) {child = [item].        Childarray OBJECTATINDEX:I]; [_deletearry addobject:child];//added to the delete array} item.    Open = no;//Set child view off Nsmutablearray *mutablearry = [Nsmutablearray array];        Nsmutableindexset *set= [Nsmutableindexset indexset];//set the index used to hold the deleted 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];//calls the function to remove the return mutablearry from the array;} @end


This is an example of a two-level menu that is referenced in the CSDN download.

But some different, if his code you do not understand, I understand the more to see him is simple;

Can download my source code to run a look; http://download.csdn.net/detail/u010123208/7685367

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.