UITableView 数据未显示

UITableView data is not showing

我创建了一个自定义 class ("customClass"),它是 UIView 的子class。然后我在它的子视图中添加了一个 tableView,并为 datasoucre 创建了一个 array。我将 delegatedatasource 设置为 self。然后在storyboard里,我设置了一个UIViewsclass到customClass.

然后在 mainVC class 中,我将数组设置为一些 strings 并将 array 设置为 customClass 中的数组。当我 运行 应用程序时,它不会崩溃或给我任何错误,但我在 tableView

中看不到任何结果

mainVC.m 中的设置方式如下:

self.myArray = [[NSArray alloc] initWithObjects:@"First", @"Second", @"Third", @"Fourth", @"Fith", @"Sixths", @"Seventh", nil];
self.myView.resultArray = self.myArray;
[self.myView.tableView reloadData];

这是 customClass 中的代码:

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        [self setupTableView];
    }
    return self;
}

- (void)setupTableView
{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;

    self.tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
    [self addSubview:self.tableView];
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.resultArray count];
}

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

    [cell.textLabel setText:self.resultArray [indexPath.row]];

    return cell;
}

当我 运行 应用程序时,我没有在 tableView 中看到任何结果,尽管我确实看到 tableView 显示。我做错了什么,我该如何解决?

编辑

刚刚意识到,cellForRow 甚至没有被调用!我做了一个 reloadData,它仍然没有被调用。

您可以将customClass的backgroundColor设置为红色,然后检查customClass是否添加到mainVC的视图中。

initWithFrame: - It is recommended that you implement this method. You can also implement custom initialization methods in addition to, or instead of, this method.

initWithCoder: - Implement this method if you load your view from an Interface Builder nib file and your view requires custom initialization.

initWithCoder is called much before init and viewDidLoad methods

在 MainVC 中

viewClass *vc = [[viewClass alloc] initWithFrame:self.view.frame];
vc.resultArray = [[NSArray alloc] initWithObjects:@"First", @"Second", @"Third", @"Fourth", @"Fith", @"Sixths", @"Seventh", nil];
[vc.tableView reloadData];
[self.view addSubview:vc];

自定义 Class...

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

    if (self) {
        [self setupTableView];
    }
    return self;
}


- (void)setupTableView
{

    self.tableView = [[UITableView alloc] initWithFrame:self.frame style:UITableViewStylePlain];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self addSubview:self.tableView]; // Datasource and delegate after alloc init
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.resultArray count];
}

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

    [cell.textLabel setText:self.resultArray [indexPath.row]];

    return cell;
}

使用自定义UItableviewCell时注册Cell,否则不要使用[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];,会崩溃