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