相信很多朋友都寫過這樣的設置界面,點擊每個cell 都會調到相應的新的控制器,如果說每個控制器我都要在里面進行重寫布局,那么代碼量肯定會非常的大而且閱讀性也不是很好.

就像這樣(下圖),這里push的沒一個控制器都是一個單獨的控制器 , 這個設置界面包含五個控制器 , 每個里面又要單獨的進行設置 , 這個還算是少的,如果說這個設置界面進去后 , 還可以跳轉要是還這樣寫那真的是 ........ 醉了 , 可能你整個項目的界面數(shù)量基本2/3 都在設置界面上了.大大的提高了工作效率,而且程序的可靠性可能也會受到相應的影響

這個問題困擾我很久(上面這個就是我寫的 醉了當時著急 ?沒時間細想 ?就先把功能實現(xiàn)再說)想找相關的技術博客發(fā)現(xiàn)這方面的博客實在是太少了終于讓我找到了解決辦法(sorry,又不要臉了,看到了個視頻里面的方法) plist文件加載控制器 而且可以為控制器進行設置,本來可以寫跟設置界面一樣的,但是周末犯懶了,等我想寫的時候已經(jīng)12點多了,沒辦法周一還要上班就寫了個簡易版的,先對付看吧下周補全(下面就是plist文件,自己寫)

排版有點問題 , 下次肯定改進
```
#import "ViewController.h"#import "SetingViewController.h"@interface ViewController ()@property (nonatomic,strong)NSArray *arr;
@end
@implementation ViewController
-(NSArray *)arr{
if (!_arr) {
NSString *path = [[NSBundle mainBundle] pathForResource:@"SetingV_C.plist" ofType:nil];
_arr = [NSArray arrayWithContentsOfFile:path];
}
return _arr;
}
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"%lu",(unsigned long)self.arr.count);
UITableView *tab = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
[self.view addSubview:tab];
tab.delegate = self;
tab.dataSource = self;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *ID = @"cell";
NSDictionary *dict = self.arr[indexPath.row];
NSLog(@"%@",dict);
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:dict[@"accview_img"]]];
cell.accessoryView.backgroundColor = [UIColor blackColor];
cell.textLabel.text = @"cell";
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 80;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSDictionary *dict = self.arr[indexPath.row];
NSString *TargetClassName = dict[@"setingV_c"];
if (TargetClassName) {
Class TargetClass = NSClassFromString(TargetClassName);
UIViewController *vc = [[TargetClass alloc]init];
if ([vc isKindOfClass:[SetingViewController class]]) {
SetingViewController *setVc = (SetingViewController *)vc;
[self.navigationController pushViewController:setVc animated:YES];
NSLog(@"%@",[setVc class]);
}
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
```
上面看不清楚的 看這里吧 ?首先懶加載吧plist文件加載到數(shù)組中(數(shù)組中是字典 ?tabView的數(shù)據(jù)源和代理方法就不粘貼了 基本就那樣)

點擊cell的時候 ?通過數(shù)組找到相應的鍵值,再把他們通過映射轉正類然后就可以直接跳轉,頁面布局相近的控制器可以重復使用,只需要修改(換一個plist)為他重新設置頁面就可以使用

(下圖就是這個代碼的運行狀態(tài),accessoryView 添加的圖片沒時間找我就自己截了個圖演示用用就OK,這些都是通過plist文件加載的,除了文字cell, 那個是著急了,沒時間弄) 如果需要我們調到下個控制器還可以用這樣的布局,對應的accessoryView 或者其他的cell中的顯示的內(nèi)容我們可以通過修改plist文件就可以進行修改
