Simple implementation of nested tables in a table (UITableView) in iOS, iosuitableview
Simple implementation of UITableView nested tables in iOS
First, let's talk about the idea: we define two tableviews in a controller, one as the nested rootTable and the other as the nested table tableView1. Then we need to implement UITableViewDelegate, UITableViewDataSource, how can we differentiate between Proxies? In fact, there are two methods: one is to set the tag value for the two tableviews defined, and the other is to implement the proxy when tableview = rootTable is directly written, otherwise, the proxy method of tableView1 is implemented.
Test Environment Xcode6.1
Demo:
The following is the implementation code:
ViewController. h # import <UIKit/UIKit. h> @ interface ViewController: UIViewController <strong, UITableViewDataSource> {UITableView * rootTable; UITableView * tableView1; NSMutableArray * ChildArr; // optional * ChildArr1; // NSMutableArray * ChildArr2 ;} @ endViewController. m # import "ViewController. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // Do any 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, 65, 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] initWithFrame: 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) didreceivemorywarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} @ end