UITableViewController not loading data

前端 未结 4 1301
误落风尘
误落风尘 2020-12-22 12:36

So I have created a subclass of UITableViewController and set the delegate and datasource to itself. But still the methods like cellForRowAtIndexPath

相关标签:
4条回答
  • 2020-12-22 12:54

    If you are extending the UITableViewController it already creates UITableView instance at viewDidLoad. Try to set a breakpoint there and check it in debugger console:

    po [self view]
    

    Therefore you should not create the UITableView instance by yourself at this point. If you need a custom init of the table view you should do it earlier

    EDIT

    From the documentation:

    About Table Views in iOS-Based Applications

    The easiest and recommended way to create a table view is to create a custom UITableViewController object. UITableViewController creates the table view and assigns itself as delegate and data source.

    Creating a Table View Programmatically

    If you choose not to use UITableViewController for table-view management, you must replicate what this class gives you “for free.”

    - (void)loadView // <-- NOT VIEW DID LOAD!
    {
        UITableView *tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]
                                      style:UITableViewStylePlain];
        tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
        tableView.delegate = self;
        tableView.dataSource = self;
        [tableView reloadData];
    
        self.view = tableView;
        [tableView release];
    }
    
    0 讨论(0)
  • 2020-12-22 13:04

    In .h file, add UITableViewDelegate and UITableViewDataSource

    Something like this:

    @interface Yourclass : rootClass <UITableViewDelegate, UITableViewDataSource>
    
    0 讨论(0)
  • 2020-12-22 13:08

    try this one in .h file

    @interface KLActionsViewController:UIView <UITableViewDelegate,UItableViewDataSource>
    
    0 讨论(0)
  • 2020-12-22 13:16

    Try adding [self.tableView reloadData] at the end of the viewDidLoad

    0 讨论(0)
提交回复
热议问题