Add a UITableView to an existing ViewController

ε祈祈猫儿з 提交于 2019-12-23 16:32:27

问题


I'm trying to add a UITableView to an existing UIViewController programmatically.

My run.h file has this:

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

@property (strong, nonatomic) UITableView *tableView;

My app runs through a speed test and at the end of the test I'm trying to add a TableView to the View. At the moment I have this:

-(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;
    }

The app never makes it into the cellForRowAtIndexPath method but does make it into the other two above. I can't see any error in the Xcode output.

Anything I need to be doing here:

When the app fails all I get is this:


回答1:


If width of UITableView is zero, it will never call datasource

So change your code like this

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

Good luck!




回答2:


You should init your UITableView with a frame (and optionally a style), or at least define the frame somewhere.

For example:

UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 200, 200) style:UITableViewStylePlain];



回答3:


You need to assign the property tableView to the instance you are creating in the addSpeedTable method, then reference it using self.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];
}

Edit

As pointed out by the other answers, you'll probably need to set the frame on the UITableView as well:

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



回答4:


You use init method to initialize a view, which means the view's frame is CGRectZero. So the numberOfRowsInSection is called but SDK finds no cell need to be shown as the frame is CGRectZero.

-(void)addSpeedTable {

.....

tableView.frame = self.view.bounds;
[self.view addSubview:tableView];

}

What's more you should assign your tableView to the property by self.tableView = tableView or your tableView property will never be valid state.




回答5:


You can add breakpoint at the thirdLine, and print out the frame see if one of its bounds is 0.




回答6:


I think you may have declare the wrong syntax. I can't upload image, So notice the difference the code below. The capitalization may be the problem. { // 服务条款 static NSString * cellId = @"justCommon";

    static NSString *CellIdentifier = @"newCell";
}


来源:https://stackoverflow.com/questions/28844921/add-a-uitableview-to-an-existing-viewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!