Are there Anyone that use TableViewController without subclassing?

后端 未结 4 1741
臣服心动
臣服心动 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: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.

提交回复
热议问题