简体   繁体   中英

Static Table in a UIViewController

Pior Xcode 5.1, I had a Static Table view with two cells. I was simply using it in as the login form, username and password.

Now that I upgraded to Xcode 5.1 I am getting an error: Static table views are only valid when embedded in UITableViewController instances

I found a few work arounds but they were all old a none of them helped me. My View Controller class is a subclass of the UIViewController class. I am also implementing the following two methods:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

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

    switch (indexPath.row) {
        case 0:
            cell = self.tableViewCellUser;
            break;
        case 1:
            cell = self.tableViewCellPassword;
            break;
    }
    return cell;
}

This was working perfectly, but for some reason Xcode 5.1 broke it... Using a container seem to be a overkill as I would need a new view controller just because of two properties...

Static cells are only valid in a UITableViewController. Not in a UITableView. If you used the UITableview instead of the UItableViewController because the layout shouldn't be fullscreen than add a container view to the view you want to place it instead of the table view in the storyboard. Create a tableViewController in the storyboard. Control drag from the containerView to te TableViewController and release. select 'embed' from the popup.

I am also implementing the following two methods:

Are you sure that your code included those methods when it was working? If you're using a static table, you shouldn't implement any of the data source methods. Look, it says so right here :

Note: If a table view in a storyboard is static, the custom subclass of UITableViewController that contains the table view should not implement the data source protocol. Instead, the table view controller should use its viewDidLoad method to populate the table view's data.

I had similar trouble myself recently -- I created a static table and found that it didn't work (cells didn't show up properly) because I had inadvertently implemented the same methods that you have.

Also, as some of the comments indicate, you should be using an instance of UITableViewController or a subclass. It's hard to see why this might be a problem for you -- you can do anything in a table view controller that you can do in a regular old view controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM