主题:功能的代码实现与storyboard实现
问题描述:当在父视图ViewController(继承自WMPageController)文件中以代码(alloc init)实现子视图newTableViewController,子视图中的cell也采用代码化的定义,子视图确实如期呈现,而newTableViewCell的cell.label定义于storyboard中,而在stroyboard中并未初始化newTableViewController,所以没有初始化的cell.label无法赋值。
问题代码:
ViewController.m
- (UIViewController *)pageController:(WMPageController *)pageController viewControllerAtIndex:(NSInteger)index {
switch (index) {
case 0: {
newTableViewController *vc = [[newTableViewController alloc] init];
return vc;
}
break;
case 1: {
oldViewController *vc = [[oldViewController alloc] init];
return vc;
}
break;
case 2: {
blackViewController *vc = [[blackViewController alloc] init];
return vc;
}
break;
default: {
return [[blackViewController alloc] init];
}
break;
}
}
newTableViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
[self.tableView registerClass:[newTableViewCell class] forCellReuseIdentifier:@"newCell"];
newTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"newCell" forIndexPath:indexPath];
cell.cellLabelc = @"123";
return cell;
}
newTableViewCell.m
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
解决方案:
用storyboard构建cell
1.父视图ViewController中实现newTableViewController:将storyboard中的newTableViewController给identitier:newT,
2.父视图ViewController中实现newTableViewController:将storyboard中控制器与代码联系
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
return [sb instantiateViewControllerWithIdentifier:@"newTableViewController"];
3.删除newTableViewController中的代码化定义cell方法(此处重复定义了cell,因为在storyboard中已经写了)
解决代码:
ViewController.m
- (UIViewController *)pageController:(WMPageController *)pageController viewControllerAtIndex:(NSInteger)index {
switch (index) {
case 0: {
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
newTableViewController *tv = [sb instantiateViewControllerWithIdentifier:@"newT"];
tv.newsId = @"32";
return tv;
}
break;
case 1: {
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
newTableViewController *tv = [sb instantiateViewControllerWithIdentifier:@"newT"];
tv.newsId = @"34";
return tv;
}
break;
case 2: {
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
newTableViewController *tv = [sb instantiateViewControllerWithIdentifier:@"newT"];
tv.newsId = @"33";
return tv;
}
break;
case 3: {
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
newTableViewController *tv = [sb instantiateViewControllerWithIdentifier:@"newT"];
tv.newsId = @"70";
return tv;
}
break;
case 4: {
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
newTableViewController *tv = [sb instantiateViewControllerWithIdentifier:@"newT"];
tv.newsId = @"46";
return tv;
}
break;
case 5: {
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
newTableViewController *tv = [sb instantiateViewControllerWithIdentifier:@"newT"];
tv.newsId = @"69";
return tv;
}
break;
case 6: {
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
newTableViewController *tv = [sb instantiateViewControllerWithIdentifier:@"newT"];
tv.newsId = @"51";
return tv;
}
break;
default: {
return [[blackViewController alloc] init];
}
break;
}
}
newTableViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
newTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"newCell" forIndexPath:indexPath];
cell.cellLabelc = @"123";
return cell;
}
newTableViewCell.m
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
总结:在storyboard中写就一直在storyboard中写,纯代码写就一直纯代码写,避免出错。
本次问题就在于创建的cell.label是用storyboard写的(在storyboard中以图形化编程定义),但写代码的时候返回了一个未定义在storyboard中的视图,故也没法初始化以storyboard定义的cell。
我以为我创建好的视图中定义好了cell,实际上并没有,因为是用纯代码创建的,而我的cell定义在storyboard中,而我的代码没有跟storyboard产生联系(需要给storyboard中定义cell的视图newTableViewController给identifier)
需要注意的一点:storyboard定义与纯代码定义不要重复,会有逻辑混乱