ios - 将 UITableView 添加到现有的 ViewController

标签 ios objective-c uitableview addsubview tableviewcell

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

我的 run.h 文件有这个:

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

@property (strong, nonatomic) UITableView *tableView;

我的应用程序通过了速度测试,在测试结束时,我尝试向 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;
    }

该应用从未进入 cellForRowAtIndexPath 方法,但确实进入了上面的其他两个方法。我在 Xcode 输出中看不到任何错误。

我需要在这里做的任何事情:

enter image description here

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

enter image description here

最佳答案

如果 UITableView 的宽度为零,它永远不会调用 datasource

所以像这样更改你的代码

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

祝你好运!

关于ios - 将 UITableView 添加到现有的 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28844921/

相关文章:

ios - UITableView 不重复使用 Storyboard的单元格

iphone - 如何在iOS中绘制如下所示的项目?

android - iOS 相当于 Android 的均衡器类

iOS 和 XCode : What is the best way to store connection settings (i. URL)用于 iOS 应用程序?

iphone - arc4random 随机数生成器

ios 以毫秒为单位向 UITableView 添加数据

ios - ECSlidingViewController 在设备方向变为横向时隐藏

iphone - Storyboard 不显示 Xcode iOS 选项卡

ios - 如何将 XIB 文件中的 UIView 作为 subview 添加到另一个 Xib 文件

ios - 如何根据来自服务器的标志更改 uitableviewcell 的颜色