原文出處: 陳燕翔(@燕翔de專注) 歡迎分享原創到伯樂頭條
is Initial View Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | /** * 添加所有子控制器 */ - (void)setUpAllChildViewController{ // 1.添加第一個控制器 CYXOneViewController *oneVC = [[CYXOneViewController alloc]init]; [self setUpOneChildViewController:oneVC image:[UIImage imageNamed:@"tab_home_icon"] title:@"首頁"]; // 2.添加第2個控制器 CYXTwoViewController *twoVC = [[CYXTwoViewController alloc]init]; [self setUpOneChildViewController:twoVC image:[UIImage imageNamed:@"js"] title:@"技術"]; // 3.添加第3個控制器 CYXThreeViewController *threeVC = [[CYXThreeViewController alloc]init]; [self setUpOneChildViewController:threeVC image:[UIImage imageNamed:@"qw"] title:@"博文"]; // 4.添加第4個控制器 // 4.1 初始化并從Storyboard中加載控制器 UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"CYXFourViewController" bundle:nil]; // 4.2 關聯storyBoard與CYXFourViewController CYXFourViewController *fourVC = [storyBoard instantiateInitialViewController]; [self setUpOneChildViewController:fourVC image:[UIImage imageNamed:@"user"] title:@"設置"]; } |
1 2 3 4 5 6 7 8 | @interface CYXGroupItem : NSObject /** 頭部標題 */ @PRoperty (strong, nonatomic) NSString * headerTitle; /** 尾部標題 */ @property (strong, nonatomic) NSString * footerTitle; /** 組中的行數組 */ @property (strong, nonatomic) NSArray * items; @end |
1 2 3 4 | @interface CYXSettingItem : NSObject @property (strong, nonatomic) NSString * title;/**< 標題 */ + (instancetype)itemWithtitle:(NSString *)title;/**< 設置標題值 類方法 */ @end |
1 2 3 4 5 | + (instancetype)itemWithtitle:(NSString *)title{ CYXSettingItem *item = [[CYXSettingItem alloc]init]; item.title = title; return item; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | #import "CYXFourViewController.h" #import "CYXSettingItem.h" #import "CYXGroupItem.h" @interface CYXFourViewController () @property (strong, nonatomic) NSMutableArray * groups;/**< 組數組 描述TableView有多少組 */ @end @implementation CYXFourViewController /** groups 數據懶加載*/ - (NSMutableArray *)groups { if (!_groups) { _groups = [NSMutableArray array]; } return _groups; } - (instancetype)init{ // 設置tableView的分組樣式為Group樣式 return [self initWithStyle:UITableViewStyleGrouped]; } - (void)viewDidLoad { [super viewDidLoad]; //添加第1組模型 [self setGroup1]; //添加第2組模型 [self setGroup2]; //添加第3組模型 [self setGroup3]; } - (void)setGroup1{ // 創建組模型 CYXGroupItem *group = [[CYXGroupItem alloc]init]; // 創建行模型 CYXSettingItem *item = [CYXSettingItem itemWithtitle:@"我的賬號"]; CYXSettingItem *item1 = [CYXSettingItem itemWithtitle:@"我的收藏"]; // 保存行模型數組 group.items = @[item,item1]; // 把組模型保存到groups數組 [self.groups addObject:group]; } - (void)setGroup2{ CYXGroupItem *group = [[CYXGroupItem alloc]init]; CYXSettingItem *item = [CYXSettingItem itemWithtitle:@"我去好評"]; CYXSettingItem *item1 = [CYXSettingItem itemWithtitle:@"我去吐槽"]; group.items = @[item,item1]; [self.groups addObject:group]; } - (void)setGroup3{ CYXGroupItem *group = [[CYXGroupItem alloc]init]; CYXSettingItem *item = [CYXSettingItem itemWithtitle:@"關注我們"]; CYXSettingItem *item1 = [CYXSettingItem itemWithtitle:@"關于我們"]; group.items = @[item,item1]; [self.groups addObject:group]; } #pragma mark - TableView的數據源代理方法實現 /** * 返回有多少組的代理方法 */ - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.groups.count; } /** * 返回每組有多少行的代理方法 */ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { CYXGroupItem *group = self.groups[section]; return group.items.count; } /** * 返回每一行Cell的代理方法 */ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 1 初始化Cell // 1.1 設置Cell的重用標識 static NSString *ID = @"cell"; // 1.2 去緩存池中取Cell UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; // 1.3 若取不到便創建一個帶重用標識的Cell if (cell == nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; } // 設置Cell右邊的小箭頭 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; // 2 設置數據 // 2.1 取出組模型 CYXGroupItem *group = self.groups[indexPath.section]; // 2.2 根據組模型取出行(Cell)模型 CYXSettingItem *item = group.items[indexPath.row]; // 2.3 根據行模型的數據賦值 cell.textLabel.text = item.title; return cell; } @end |
實現效果
1 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; |
看到這里,如果你是個iOS初學者,應該覺得方式2的實現過于繁瑣,但方式2卻是個一勞永逸的方式。換句話說,如果你用方式2封裝了一個完整的設置頁面的框架,在下一個項目中,再有類似的設置頁面,你便可以直接把這個框架拷貝過去,改少量的代碼便可以完美兼容,肯定比你再重新拖Storyboard要便捷,因此本人是比較推崇這種方式的。
全能程序員交流QQ群290551701,群內程序員都是來自,百度、阿里、京東、小米、去哪兒、餓了嗎、藍港等高級程序員 ,擁有豐富的經驗。加入我們,直線溝通技術大牛,最佳的學習環境,了解業內的一手的資訊。如果你想結實大牛,那 就加入進來,讓大牛帶你超神!
新聞熱點
疑難解答