سؤال

I've got a ViewController that has a UITableView within it. When I'm watching tutorials people are using things like this:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _Title.count;
}

How am I able to generate the stubs without firstly creating the class with them in. When I made the class I selected it as a UIViewController. I've been playing around trying to auto generate the stubs but all to no avail.

هل كانت مفيدة؟

المحلول

Simply add the UITableViewDataSource (and most likely the UITableViewDelegate) to your UIViewController declaration. Example:

// MyViewController.h

@interface MyViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
// ...
@end

After that your implementation file MyViewcontroller.m should help you with the code completion.

One note: don't forget to set yourself as dataSource: _tableview.dataSource = self;

نصائح أخرى

If you added the tableview by code, you need to create a property (weak) in order to have a reference to your table view after adding it to your view controller's subview. If you add it by using interface builder, you need to create a iboutlet property that will allow you to "bind" your table view property with the xib/storyboard file representing your view controller. Alternatively, you can use UITableViewController as the parent class of your view controller. This class already has a property to access the table view in your view controller.

Tell your controller that you need to conform to the table view protocols and they will start to auto-complete when you try to type them in. You can check the docs of a protocol to find the available methods. Checking the UITableView docs would tell you about the relevant data source and delegate:

The data source must adopt the UITableViewDataSource protocol and the delegate must adopt the UITableViewDelegate protocol.

In your header file:

@interface MyViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

You have a couple of options.

You could make your class inherit from UITableViewController instead of UIViewController. This will give you a tableView so you don't need to make one.

Or...

Your UIViewController could implement the protocols UITableViewDataSource and UITableViewDelegate. Then set the dataSource and delegate properties of your table view to self (your view controller containing the table).

-First of all you may need to add datasource and delegate of UITableViewController in your UIViewController header file

@interface MyViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

and then implement the required and optional methods to populate the data in your _tableView.

Sample Code for TableView demonstration by Apple: https://developer.apple.com/library/ios/samplecode/TableViewSuite/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007318

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top