Heim > Fragen und Antworten > Hauptteil
项目演示GIF图:
http://ww1.sinaimg.cn/large/70421ae5jw1f43m036b22g20b50iknpd.gif
应该怎么做,我知道要这个项目要用2个scrollView.
"最新"这个页面是在stroyboard里面创建的视图,
"视频"是用代码创建再用通知的方法实现点击cell跳转新页面的.
"最新"页面点击cell的跳转代码:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
latestWebView *webView = [[latestWebView alloc] init];
webView.hidesBottomBarWhenPushed = YES;
webView.urlDetial = self.urlAry[indexPath.row];
[self.navigationController pushViewController:webView animated:YES];
NSLog(@"navigationController: %@",self.navigationController);
}
"视频"页面的点击cell跳转代码:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[[NSNotificationCenter defaultCenter] postNotificationName:@"noHideen" object:nil];
}
下面是在AppDelegate里面创建的主视图(MainViewController):
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
MainViewController * mainvc = [[MainViewController alloc] init];
UINavigationController * nvc = [[UINavigationController alloc] initWithRootViewController:mainvc];
self.window.rootViewController = nvc;
[self.window makeKeyAndVisible];
MainViewController的scrollView里面添加的各个视图:
[self.vcScrollview addSubview:self.nearbyVC.view];
[self.vcScrollview addSubview:self.latestVC.view];
[self.vcScrollview addSubview:self.videoVC.view];
[self.vcScrollview addSubview:self.collectionVC.view];
这样做以后发现点击tableView的Cell的时候push不了新页面(打印没有navigation),要用通知的方法才能实现push跳转:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tiaozhuan:) name:@"tiao" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tiaozhuan:) name:@"noHideen" object:nil];
(void)tiaozhuan:(NSNotification *)notifiction
{
NSLog(@"%@",notifiction.object);
if (notifiction.object == nil) {
self.barScrollview.hidden = NO;
}
else{
testViewController * tes = [[testViewController alloc] init];
self.barScrollview.hidden = YES;
[self.navigationController pushViewController:tes animated:YES];
}
}
1:我如果一定要实现点击cell以后push一个新页面. 是不是只能用通知的方法去实现?
2:不然的话只能在MainViewController里面创建几个tableView了(这样MainViewController肯定会很臃肿).
3:这个项目真的很简单,被困扰了一个星期,作为初学者很受打击,麻烦各位帮帮忙,或者您有更好的办法也请告知,谢谢.
描述得有点乱,万分感谢.需要源码的加我QQ 1404565175
迷茫2017-04-18 09:07:55
有几个问题
关于custom contain viewcontroller 你需要去了解一下 这里你会明白 self.navigationController是获取不到navigationController的 应该修改为self.parentViewController.navigationController
然后addSubview时 mainVC需要addChildViewController 已经willMoveToParentViewController等相关方法的调用
这个需求很大一个是关于n个viewcontroller的管理 如何保证性能 需要让它Reusable 不能一次就全部加载
mainVC需要组件化 自己定义一个contain viewcontroller 来管理下边滚动的viewController (实现后再去优化好了)
网上模仿网易新闻的demo很多 你可以学习一下
大家讲道理2017-04-18 09:07:55
不跳传的话。
1.self.navigationController 可以为空,你打印一下。为空的话你这个控制器不是navigationController了。所以跳不了。
-(void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath ;没调用
黄舟2017-04-18 09:07:55
"最新"页面点击cell中self.navigationController是tableview所在的控制器,不是mainVC,要获取到main的导航控制器然后跳转