当主视图控制器的子视图时,tableview不会滚动

时间:2013-10-28 05:48:26

标签: ios uitableview uiviewcontroller uistoryboardsegue

我有一个MainViewController,当单击视图底部的标签栏按钮时,它会通过segue使用另一个名为dataViewController的视图控制器填充MainViewController中的中央主视图。在dataViewController中,它有一个从数组填充的UITableView。我错过了一些简单的想法,因为当我给出dataViewcontroller初始场景视图控制器时,它运行良好,我可以滚动表及其所有单元格/内容。一旦我将MainViewController作为初始场景视图控制器,我点击数据按钮,它就会改变视图填充tableview。我可以选择单个单元格并突出显示,但是当我试图翻阅表格时,它会使模拟器崩溃并确实给出看起来像是什么原因?

MainViewController.h

#import <UIKit/UIKit.h>

@interface MainViewController : UIViewController

@property (weak,nonatomic)UIViewController *currentViewController;
@property (weak, nonatomic) IBOutlet UIView *mainView;

@end

MainViewController.m

#import "MainViewController.h"

@interface MainViewController ()

@end

@implementation MainViewController

@synthesize currentViewController;
@synthesize mainView;

- (void)viewDidLoad {
    [super viewDidLoad];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"location"] == NULL) [defaults setObject:@"Spring Hill" forKey:@"location"];
    if ([defaults objectForKey:@"temperature"] == NULL) [defaults setObject:@"Celsius" forKey:@"temperature"];
    if ([defaults objectForKey:@"windSpeed"] == NULL)   [defaults setObject:@"kph" forKey:@"windSpeed"];
    if ([defaults objectForKey:@"wingType"] == NULL)    [defaults setObject:@"Paraglider" forKey:@"wingType"];
    [defaults synchronize];
}

- (void) viewWillAppear:(BOOL)animated {
    [self performSegueWithIdentifier:@"graphSegue" sender:self];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    self.navigationItem.title = [defaults objectForKey:@"location"];
}

@end

CustomTabBarSegue.h

#import <UIKit/UIKit.h>

@interface CustomTabBarSegue : UIStoryboardSegue

@end

CustomTabBarSeque.m

#import "CustomTabBarSegue.h"
#import "MainViewController.h"

@implementation CustomTabBarSegue

- (void) perform {
    MainViewController *currentView = (MainViewController *)self.sourceViewController;
    UIViewController *destView = (UIViewController *) self.destinationViewController;
    for(UIView *view in currentView.mainView.subviews){
        [view removeFromSuperview];
    }
    currentView.currentViewController = destView;
    [currentView.mainView addSubview:destView.view];
}

@end

我不确定故事板上是否有某些东西我没有勾选/选择或者我的代码不够?感谢。

1 个答案:

答案 0 :(得分:0)

在.header文件中,您必须指定此视图符合UITableView协议,如下所示:

@interface MainViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

虽然您的代码中似乎没有声明UITableView。您必须将其声明为属性,而不是将其放在storyboard中并在viewDidLoad方法中设置其委托和数据源,如下所示:

self.myTableView.delegate = self;
self.myTableView.datasource = self;