将UITableView添加到现有的ViewController

Dan*_*mer 3 objective-c uitableview addsubview tableviewcell ios

我正在尝试以编程方式添加UITableView到现有的UIViewController.

我的run.h文件有这个:

@interface runTests : UIViewController <UINavigationControllerDelegate,UITextFieldDelegate,UIAlertViewDelegate,UITableViewDelegate,UITableViewDataSource> {NSTimer *Timer;}

@property (strong, nonatomic) UITableView *tableView;
Run Code Online (Sandbox Code Playgroud)

我的应用程序运行速度测试,在测试结束时,我正在尝试向View添加TableView.目前我有这个:

-(void)addSpeedTable {


    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;

    self.tableView.rowHeight = 45;
    self.tableView.sectionFooterHeight = 22;
    self.tableView.sectionHeaderHeight = 22;

    self.tableView.delegate = self;
    self.tableView.dataSource = self;

    [self.view addSubview:self.tableView];


}

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

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

    {
        return 1;
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"newCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        }

        cell.textLabel.text = @"Testing";

        return cell;
    }
Run Code Online (Sandbox Code Playgroud)

该应用程序永远不会进入该cellForRowAtIndexPath方法,但确实进入上面的其他两个.我在Xcode输出中看不到任何错误.

我需要做的任何事情:

在此输入图像描述

当应用程序失败时,我得到的是:

在此输入图像描述

Ton*_*ony 6

如果width UITableView为零,则永远不会调用datasource

所以改变你的代码就像这样

self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
Run Code Online (Sandbox Code Playgroud)

祝好运!