【问题标题】:numberofSections method not getting called by [tableview reloadData][tableview reloadData] 未调用 numberofSections 方法
【发布时间】:2013-12-05 06:29:34
【问题描述】:

我正在使用xcode 5.0.2。它是一个UItabBarcontroller2 tabs,每个tabApp Delegate 中以编程方式嵌入一个navigation controller。这个问题可能比看起来要复杂一些。我正在使用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 and datasource 连接到文件所有者。

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 methods 没有被调用。只有viewDidLoad 被调用。我应该改变什么?

second tab 有一个view-controller,它显示一个web-view。 在情节提要中,segue 是 PUSH,所以我必须为第一个 VC 提供 Navigationcontroller

【问题讨论】:

  • @βḧäṙℊặṿῗ:见 viewDidLoad 中的第二行
  • 只是一点:你不需要这样做:[self.view addSubview:_tableVC];如果你已经有 nib 的 IBOutlet。
  • 你的 .h 文件中有类似 :@interface AIFirstViewConroller : UIView 的东西吗?
  • 顺便说一句,你正在做很多 reloadData !!,你不需要在 viewDidLoad 中这样做。
  • 如果您使用故事板,为什么要使用 initWithNibName:bundle: 来实例化您的控制器?

标签: ios uinavigationcontroller uitabbarcontroller uitableview


【解决方案1】:

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

2) 您是否检查过,您是否将添加的表格视图(您的 .xib)与您的表格视图的实例方法 tableVC 相关联?

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

4) 您不需要在 viewDidLoad重新加载表格视图。您总是在表视图的数据源发生变化的地方执行 reloadData

5) 你是否实现了委托函数-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 来根据你的需要制作你的单格?

【讨论】:

  • Hammy 我已经完成了以上所有操作,我认为删除 reloadData 命令不会解决问题。
  • @ShaluRaj 有什么解决办法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-23
  • 1970-01-01
相关资源
最近更新 更多