Are there Anyone that use TableViewController without subclassing?

后端 未结 4 1725
臣服心动
臣服心动 2021-01-22 00:45

I am just curious. In IB, we can put a tableviewcontroller. However, as far as I know, we always subclass that tableview controller right? That way we can implement delegate, et

相关标签:
4条回答
  • 2021-01-22 01:22

    Whether you use a subclass of UITableViewController or UIViewController you need to set the data your table is going to display, otherwise, what's the point of a blank table? To achieve that you have to subclass and implement some methods. It's also a good idea to keep the delegate and the datasource in the same controller, unless the complexity really asks for different classes.

    That being said, I always create my own table controllers as a subclass of UIViewController and implement the table controllers methods myself, because it gives you more flexibility. Matt Gallagher has several posts on how and why. See UITableView construction, drawing and management (revisited).

    If you want to give it a try, create a subclass of UIViewController with a XIB and add the following sample code:

    // interface
    #import <UIKit/UIKit.h>
    @interface SettingsVC : UIViewController <UITableViewDelegate, UITableViewDataSource> 
    @property (nonatomic, retain) IBOutlet UITableView *tableView;
    @property (nonatomic, retain) NSMutableArray *array;
    @end
    
    // implementation
    @synthesize tableView = _tableView;
    @synthesize array = _array;
    # pragma mark - UITableViewDataSource
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return 1;
    }
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return [self.array count];
    }
    - (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        int row = [indexPath row];
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }
        cell.textLabel.text = [self.array objectAtIndex:row];
        return cell;
    }
    

    Then add a UITableView object to the XIB, link the tableView of the controller to the UITableView object, and link the delegate and datasource of the UITableView to the controller.

    0 讨论(0)
  • 2021-01-22 01:36

    I don't think you can use a UITableViewController as is, it's like using a UIViewController without subclassing it : you can't set any inner mechanics.

    But you can have a UITableView without using a UITableViewController.

    0 讨论(0)
  • 2021-01-22 01:46

    Sure you can use UITableViewController without subclassing it.

    Samplecode is very easy and straight forward.

    For example like this:

    - (IBAction)selectSomeOption:(id)sender {
        UITableViewController *tableViewController = [[UITableViewController alloc] initWithStyle:UITableViewStyleGrouped];
        tableViewController.tableView.dataSource = self;
        tableViewController.tableView.delegate = self;
        tableViewController.title = "Select some option";
        [self.navigationController pushViewController:tableViewController animated:YES];
    }
    

    and the UITableViewDatasource and Delegate methods go into the same class.

    Sure, if you like pain you could create a UIViewController in code and add a tableView on your own.
    Or create a subclass for such an easy task.

    The use of a non subclassed UITableViewController is sometimes convenient.

    0 讨论(0)
  • 2021-01-22 01:48

    No, this is not necessary to inherit your class with tableViewController. You can use table view by simply putting TableViewController in xib.

    and setting its delegate and datasourse to file's owner you can draw the table cells.

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