ios - 表格单元格未显示

标签 ios uitableview

我正在开始 iOS 编程,但遇到了障碍。我有一个登录 View Controller ,它使用以下代码将控件(在检查用户身份验证等之后)重定向到选项卡 Controller (主屏幕):

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UITabBarController *viewController = (UITabBarController *)[storyboard instantiateViewControllerWithIdentifier:@"homescreen"];
    [self presentViewController:viewController animated:YES completion:nil];

主屏幕中的第一个 View Controller 是一个导航 Controller ,它通向订阅 View Controller (一个 TableView Controller )。从 app delegate.m,我试图发送一个值数组 (chefs),该表可以使用以下代码来填充自身:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UITabBarController *tabBarController = (UITabBarController *)[storyboard instantiateViewControllerWithIdentifier:@"homescreen"];
UINavigationController *navigationController = [tabBarController viewControllers][0];
SubscriptionViewController *controller = [navigationController viewControllers][0];
controller.chefs = _chefs;

但是,表格显示为空。 NSMutable 数组“chefs”已正确填充,并且在 Storyboard中,表格单元格值(UILabel 等)已正确连接到内容 View 中的对应值。作为检查,我 NSLogged 订阅 View Controller 中的 [self.chefs count] 值:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
NSLog(@"%d",[self.chefs count]);
return [self.chefs count];

}

但是,这返回了 0。我怀疑我没有正确广播数组。

编辑: 我删除了登录 View Controller 并将选项卡栏 Controller 设为 Root View Controller 。然后使用了这段代码:

UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UINavigationController *navigationController = [tabBarController viewControllers][0];
SubscriptionViewController *controller = [navigationController viewControllers][0];
controller.chefs = _chefs;

现在我可以看到正确的单元格了。我不确定我将控件从登录 View 重定向到选项卡栏 Controller 的方式是否正确,或者我使用 Storyboard ID 广播数组的方式是否正确。有什么指点吗?

最佳答案

听起来你的数组没有正确初始化,当你像这样分配它时:

controller.chefs = _chefs;

在使用 _chefs 做任何事情之前,确保它在某处被初始化:

_chefs = [[NSArray alloc]init];

关于ios - 表格单元格未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22596431/

相关文章:

ios - 如何让容器 View 受到 UINavigationBar 的影响

ios - 按下指向 TableView 的按钮时应用程序崩溃

iphone - UITableViewCell,出队和自动释放?

ios - 如何创建 UIView 以在终端中显示文本?

objective-c - shouldAutorotate 返回 true 但不旋转

ios - UISearchBar 向下移动 20 像素

iphone - 如何检查 iOS 设备是否设置为 "do not disturb"?

ios - 在 UITableViewCell 中检测具有灵活宽度的 View 的最终布局大小

ios - 如何将 UITableView 从一个 Controller 克隆到另一个 Controller 以使其保持同步?

ios - 使用 Swift 3 获取 iPhone 或 iPad 设备的 IP 地址