Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 未在以编程方式创建的tableView中加载数据_Ios_Objective C_Uitableview - Fatal编程技术网

Ios 未在以编程方式创建的tableView中加载数据

Ios 未在以编程方式创建的tableView中加载数据,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我正在尝试创建一个非常简单的应用程序,第一次不使用故事板 我只是想在应用程序加载后显示一个tableView作为我的第一个屏幕。我创建了一个UIView类,然后在viewDidLoad方法中创建了一个表视图 在委托类中,我创建了一个类的实例,然后将其设置为根视图控制器 当我运行应用程序时,会显示tableView,但单元格上不会显示数字。我一直在阅读苹果的文档,但到目前为止还没有找到解决方案。它可能很小,但我似乎无法理解 如您所见,我创建了一个数组,其中填充了非常简单的字符串,这些字符串只是数字

我正在尝试创建一个非常简单的应用程序,第一次不使用故事板

我只是想在应用程序加载后显示一个tableView作为我的第一个屏幕。我创建了一个UIView类,然后在viewDidLoad方法中创建了一个表视图

在委托类中,我创建了一个类的实例,然后将其设置为根视图控制器

当我运行应用程序时,会显示tableView,但单元格上不会显示数字。我一直在阅读苹果的文档,但到目前为止还没有找到解决方案。它可能很小,但我似乎无法理解

如您所见,我创建了一个数组,其中填充了非常简单的字符串,这些字符串只是数字。我希望每个数字都显示在表格视图中的单元格上,但当表格视图加载时,单元格为空

#import "NumbersViewController.h"

@interface NumbersViewController ()

@end

@implementation NumbersViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    UITableView *tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain];
    tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    tableView.delegate = self;
    tableView.dataSource = self;
    [tableView reloadData];

    self.view = tableView;

    self.numArray = [NSMutableArray array];
    [self.numArray addObject:@"1"];
    [self.numArray addObject:@"2"];
    [self.numArray addObject:@"3"];
    [self.numArray addObject:@"4"];
    [self.numArray addObject:@"5"];
    [self.numArray addObject:@"6"];
    [self.numArray addObject:@"7"];
    [self.numArray addObject:@"8"];
    [self.numArray addObject:@"9"];



    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Number of rows is the number of time zones in the region for the specified section.

    return [self.numArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyIdentifier = @"MyReuseIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:MyIdentifier];
    }

    NSString *number = [self.numArray objectAtIndex:indexPath.row];
    cell.textLabel.text = number;
    return cell;
}



@end

重新加载数据后添加数据

//Delegate methods called
[tableView reloadData];

self.view = tableView;

// data added
self.numArray = [NSMutableArray array];
[self.numArray addObject:@"1"];
解决方案
将值设置到数组中后,将
numArray
初始化和种子设定代码移到上面的
重新加载数据

调用表重新加载

//Delegate methods called
[tableView reloadData];

self.view = tableView;

// data added
self.numArray = [NSMutableArray array];
[self.numArray addObject:@"1"];
- (void)viewDidLoad
{
    [super viewDidLoad];

    UITableView *tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain];
    tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    tableView.delegate = self;
    tableView.dataSource = self;

    self.view = tableView;

    self.numArray = [NSMutableArray array];
    [self.numArray addObject:@"1"];
    [self.numArray addObject:@"2"];
    [self.numArray addObject:@"3"];
    [self.numArray addObject:@"4"];
    [self.numArray addObject:@"5"];
    [self.numArray addObject:@"6"];
    [self.numArray addObject:@"7"];
    [self.numArray addObject:@"8"];
    [self.numArray addObject:@"9"];

    [tableView reloadData];

}

如果你提供了一个解决方案,而不是仅仅指出问题,这将是一个更好的答案。@rmaddy我认为解决方案是显而易见的?制作一些样本数据,然后
reloadData
?:)在这种情况下,一旦指出问题,解决方案就很清楚了,但一般来说,一个好的答案可以解释问题并提供解决方案。仅供参考-无需在
viewDidLoad
方法的表中调用
reloadData
。顺便说一句-为什么不简单地使用
UITableViewController
?如果您的表视图是全屏大小的,那将是更好的选择。@rmaddy当表视图是全屏大小时,使用
UIViewController
代替
UITableViewController
实际上被认为是不好的做法吗?我选择执行
UIViewControllers
,以防UI/UX需要更改,并且
tableview
不再是屏幕大小。我只是想知道这是否真的不受欢迎。使用
UIViewController
没有什么错。如果唯一的视图是表视图,那么使用
UITableViewController
就更容易了。与您的问题无关,但这个实现令人惊讶。您正在
-viewDidLoad
中创建视图,该方法在创建控制器视图后调用。在这种情况下,您依赖于
-loadView
的默认实现来创建一个空的UIView,然后替换它。相反,覆盖
-loadView
更有意义。