ios - [tableview reloadData] 未调用 numberofSections 方法

标签 ios uinavigationcontroller uitabbarcontroller uitableview

我正在使用 xcode 5.0.2。它是一个 UItabBarcontroller,带有 2 个选项卡,每个 tab 都有一个 navigation controller 以编程方式嵌入到 App Delegate。这个问题可能比看起来有点复杂。我正在使用 UIStoryboard。是否与自动布局打开有关。

AppDelegate.m

in didFinishLaunchingMethod
{
    self.tabBarController=[[UITabBarController alloc]init];

    AIFirstViewController *viewController1 = [[AIFirstViewController alloc] initWithNibName:nil bundle:nil];
    UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
    viewController1.title = @"Menu";

    AISecondViewController *viewController2 = [[AISecondViewController alloc] initWithNibName:nil bundle:nil];
    UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
    viewController2.title = @"Search";

    self.tabBarController.viewControllers=[NSArray arrayWithObjects:navigationController1, navigationController2, nil];

    [self.window setRootViewController:_tabBarController];
    [self.window makeKeyAndVisible];
}

在第一个选项卡的 firstViewController 中,我想显示一个 tableView。我为 tableView 创建了一个 IBOutlet,它的 delegate 和 datasourcee 连接到文件所有者。

AIFirstViewConroller.m

-(void)viewDidLoad
{
    [super viewDidLoad];
    self.tableVC.delegate = self;
    self.tableVC.dataSource=self;
    _checklist = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5",  @"6", @"7", @"8", @"9", nil];
    NSLog(@"the tableview object: %@", self.tableVC);
    [self.view addSubview:_tableVC];
    [self.tableVC reloadData];
}

-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    [self.tableVC reloadData];
}

numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_checklist count];
}

tableView 方法 未被调用。只有 viewDidLoad 被调用。我应该改变什么?

第二个选项卡 有一个view-controller,它显示了一个web-view。 在 Storyboard中,segue 是 PUSH,所以我必须为第一个 VC 设置一个 Navigationcontroller

最佳答案

1) 如果您已经将 TableView 添加到相关的 .xib 文件中,则不需要将其添加为 subview ( [self.view addSubview:_tableVC])

2) 您是否检查过,您是否将添加的 TableView (您的 .xib)与 TableView 的实例方法 tableVC 链接起来了?

3) 您是否在 .h 文件中包含委托(delegate)、数据源 header ,即 <UITableViewDatasource, UITableViewDelegate>

4) 你不需要在 viewDidLoadreload table view。你总是在你的 TableView 的数据源发生变化的地方做 reloadData

5) 你有没有实现委托(delegate)函数 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 来根据你的需要制作你的单个单元格?

关于ios - [tableview reloadData] 未调用 numberofSections 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20393044/

相关文章:

ios - 将 UITabBarController 与 UINavigationController 结合使用 - Swift 3

objective-c - Objective-C : How to reload tableview immediately after switching tabs?

iOS 6 多行标签行距

ios - 如何将新对象插入到实体中并设置该对象与另一个实体的现有对象的关系?

ios - 从另一个应用程序打开 Safari 的深层链接

objective-c - 导航 Controller 标题

ios - 如何做循环和复杂的导航?

objective-c - UINavigationController 上未出现后退按钮

ios - UIWebView request.response = nil 问题

objective-c - 标签栏项目标题以编程方式未显示在 objective-c 中