Simple implementation of table (UITableView) nested tables in iOS

Source: Internet
Author: User

Simple implementation of table (UITableView) nested tables in iOS

Let's start with the idea: we define 2 TableView in a controller, one as nested roottable, and one as a nested table tableView1, so we're going to implement Uitableviewdelegate, Uitableviewdatasource, the agent of the time, how to distinguish it? Actually very simple, there are two methods, one is to define the 2 TableView set tag value, the other is directly written out TableView = = roottable when the implementation of his agent, otherwise the implementation of TableView1 proxy method.

Test environment Xcode6.1
Demo's:


Here's the code for the implementation:

Viewcontroller.h#import <UIKit/UIKit.h> @interface Viewcontroller:uiviewcontroller<uitableviewdelegate,    uitableviewdatasource>{UITableView * roottable;    UITableView * TABLEVIEW1;    Nsmutablearray * CHILDARR;    Nsmutablearray * CHILDARR1; Nsmutablearray * CHILDARR2;} @endViewController. M#import "ViewController.h" @interface Viewcontroller () @end @implementation viewcontroller-(void    ) viewdidload{[Super Viewdidload];    Additional setup after loading the view, typically from a nib.    [Self initview];    Childarr = [[Nsmutablearray alloc]initwithobjects:@ "Apple", @ "chestnut", @ "banana", @ "Pineapple", @ "Peach", @ "Litchi", nil];    Self.navigationItem.title = @ "Twotableview"; }-(void) initview{roottable = [[UITableView alloc]initwithframe:cgrectmake (0, +, self.view.frame.size.width,    self.view.frame.size.height) style:uitableviewstylegrouped];    Roottable.delegate = self;    Roottable.datasource = self;    [Self.view addsubview:roottable]; }-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{if (TableView = = roottable) {if (Indexpath.row        = = 0) {return [Childarr count]*44;        }else {return 70;    }}else {return 44;    }}-(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{if (TableView = = roottable) {return 5;    }else {return 1; }}-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (Nsinteger) section{if (TableView = =    roottable) {return 4;    }else {return [Childarr count];    }}-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{    UITableViewCell * cell = [[UITableViewCell alloc]init]; if (TableView = = roottable) {if (Indexpath.row = = 0) {tableView1 = [[UITableView alloc]init             Withframe:cgrectmake (0, 0, self.view.frame.size.width, [Childarr count]*44)]; TableView1. delegate = self;             Tableview1.datasource = self;             tableview1.scrollenabled = NO;        [Cell.contentview Addsubview:tableview1];        }else {cell.textLabel.text = @ "Roottableview";    } return cell;        }else {cell.textLabel.text = [Childarr objectAtIndex:indexPath.row];        Cell.backgroundcolor = [Uicolor Yellowcolor];    return cell; }}-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{if (TableView = =    roottable) {NSLog (@ "Roottableview");    }else {NSLog (@ "Apple");    }}-(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} @end


Simple implementation of table (UITableView) nested tables in iOS

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.