Simple implementation of nested tables in a table (UITableView) in iOS, iosuitableview

Source: Internet
Author: User

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


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.