为什么我的UITableView显示器不会?

bla*_*305 1 xcode objective-c uitableview

我已经在stackoverflow上搜索并搜索了这些内容,但未能找到解决方案.

我期待发生的事情: UITableView显示和显示包含"hi"的单元格

会发生什么: UITableView甚至不显示

viewViewController.h

#import <UIKit/UIKit.h>

@interface viewViewController : UIViewController

@property (strong, nonatomic) IBOutlet UITableView *tableView;
//note: the above property is connected to my UITableViewController

@end
Run Code Online (Sandbox Code Playgroud)

viewViewController.m

#import "viewViewController.h"

@interface viewViewController ()

@end

@implementation viewViewController
@synthesize tableView = _tableView;

- (UITableView *)tableView
{
    if (!_tableView){

        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    }
    return _tableView;
}

- (void)viewDidLoad
{
    [super viewDidLoad];


    //_tableView = [[UITableView alloc] init];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    //[self setTableView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"asking for rows");
    //#warning Incomplete method implementation.
    // Return the number of rows in the section.
    switch (section)
    {
        case 0:
            return 5;
        case 1:
            return 10;
            break;
        default:
            return 0;
            break;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.textLabel.text = @"hi";
    return cell;

}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"testtext";
}


@end
Run Code Online (Sandbox Code Playgroud)

请告诉我我做错了什么并纠正我的代码.

Tom*_*iak 5

  1. 将一些协议添加到视图控制器:

    @interface viewViewController:UIViewController

  2. 检查以确保您将delegatedatasource属性连接tableView到文件所有者.