iOS ChildViewController与View

一、概述

  在iOS中,ViewController与View是配对使用的,一个ViewController可以对应多个View,就是指View的父控制器。当然,一个ViewController也可以对应一个View,在View1中,添加另一个View1_1,使用addSubView方法,此时,也要在View1的控制器中,添加对应View1_1的ViewController控制器,使用addChildViewController方法。

二、使用

1. FirstViewController.m

 1 #import "FirstViewController.h"
 2 #import "SecondViewController.h"
 3 
 4 static NSString * const FirstReuseIdentifierCell = @"FirstIdentifierCell";
 5 
 6 @interface FirstViewController ()
 7 {
 8   UITableView *iTableView;
 9 }
10 
11 @property (nonatomic, strong) UITableView *iTableView;
12 
13 @end
14 
15 @implementation FirstViewController
16 
17 @synthesize iTableView;
18 
19 - (void)viewDidLoad
20 {
21   [super viewDidLoad];
22   CGRect frame = self.view.frame;
23   frame.origin.y = 20;
24   frame.size.height = CGRectGetHeight([UIScreen mainScreen].bounds) - 20;
25   self.iTableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
26   self.iTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
27   self.iTableView.backgroundColor = [UIColor lightGrayColor];
28   self.iTableView.delegate = self;
29   self.iTableView.dataSource = self;
30   
31   [self.view addSubview:self.iTableView];
32 }
33 
34 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
35 {
36   return 20.0f;
37 }
38 
39 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
40 {
41   return @"First View Controller";
42 }
43 
44 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
45 {
46   return 120.0f;
47 }
48 
49 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
50 {
51   return 1;
52 }
53 
54 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
55 {
56   return 3;
57 }
58 
59 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
60 {
61   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FirstReuseIdentifierCell];
62   if (!cell)
63   {
64     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FirstReuseIdentifierCell];
65   }
66   
67   SecondViewController *secondViewController = [[SecondViewController alloc] init];
68   CGRect secondFrame = CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), 90);
69   secondViewController.view.frame = secondFrame;
70   [self addChildViewController:secondViewController];
71   [cell.contentView addSubview:secondViewController.view];
72   
73   return cell;
74 }
75 
76 - (void)didReceiveMemoryWarning
77 {
78   [super didReceiveMemoryWarning];
79 }
80 
81 @end

2. SecondViewController.m

 1 #import "SecondViewController.h"
 2 
 3 static NSString * const SecondReuseIdentifier = @"SecondReuseIdentifierCell";
 4 
 5 @interface SecondViewController ()
 6 {
 7   UITableView *iTableView;
 8 }
 9 
10 @property (nonatomic, strong) UITableView *iTableView;
11 
12 @end
13 
14 @implementation SecondViewController
15 
16 @synthesize iTableView;
17 
18 - (void)viewDidLoad
19 {
20   [super viewDidLoad];
21   CGRect frame = CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), 90);
22   self.iTableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
23   self.iTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
24   self.iTableView.separatorColor = [UIColor lightGrayColor];
25   self.iTableView.backgroundColor = [UIColor lightGrayColor];
26   self.iTableView.delegate = self;
27   self.iTableView.dataSource = self;
28   
29   [self.view addSubview:self.iTableView];
30 }
31 
32 //- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
33 //{
34 //  return 20.0f;
35 //}
36 
37 //- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
38 //{
39 //  return @"Second View Controller";
40 //}
41 
42 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
43 {
44   return 30.0f;
45 }
46 
47 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
48 {
49   return 1;
50 }
51 
52 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
53 {
54   return 3;
55 }
56 
57 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
58 {
59   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SecondReuseIdentifier];
60   if (!cell)
61   {
62     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:SecondReuseIdentifier];
63   }
64   cell.backgroundColor = [UIColor redColor];
65   cell.textLabel.text = SecondReuseIdentifier;
66   return cell;
67 }
68 
69 - (void)didReceiveMemoryWarning
70 {
71   [super didReceiveMemoryWarning];
72 }
73 
74 
75 
76 @end
原文地址:https://www.cnblogs.com/naray/p/4942620.html