How can I subclass a UITableView?

前端 未结 3 1751
小鲜肉
小鲜肉 2020-12-19 10:34

I want to subclass a UITableView as I want to create a reusable table view component in my application.

The idea is instead of using a delegate for say cellForRowA

3条回答
  •  旧时难觅i
    2020-12-19 11:06

    MyTableView.h

    // MyTableView.h
    
    // This overrides the UITableViewDataSource with your own so you can add any methods   you would like.
    @protocol MyTableViewDataSource 
    
    @required
    // This is where you put methods that are required for your custom table to work (optional)
    - (int)myRequiredMethod;
    
    @optional
    // This is where you put methods that are optional, like settings (optional)
    
    @end
    
    // This overrides the UITableViewDelegate with your own so you can add any methods you would like.
    @protocol MyTableViewDelegate 
    
    @required
    // This is where you put methods that are required for your custom table to work (optional)
    
    @optional
    // This is where you put methods that are optional, like settings (optional)
    
    @end
    
    // Make sure you add UITableViewDelegate and UITableViewDataSource implementations.
    @interface MyTableView : UITableView  {
    
        // Your customer datasource and delegate.
        id  myDataSource;
        id  myDelegate;
    }
    
    @end
    

    MyTableView.m

    // MyTableView.m
    
    #import "MyTableView.h"
    
    @implementation MyTableView
    
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code
    
            // This is how you can use your custom method.
            int i = [myDataSource myRequiredMethod];
        }
        return self;
    }
    
    - (void)awakeFromNib {
        // This assigns the delegate and datasource you assigned to File's Owner in your xib to your custom methods
        myDataSource = (id)self.dataSource;
        myDelegate = (id)self.delegate;
        self.delegate = self;
        self.dataSource = self;
    }
    
    // This is an example of how to override an existing UITableView method.
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
        // This calls the method implemented in your ViewController. See Below.
        NSInteger rows = [myDataSource tableView:tableView numberOfRowsInSection:section];
    
        return rows;
    }
    
    @end
    

    MyViewController.h

    // MyViewController.h
    
    #import "MyTableView.h"
    
    // Use MyTableViewDataSource and MyTableViewDelegate instead of UITableViewDataSource and UITableViewDelegate
    @interface MyViewController : UIViewController  {
    
    @end
    

    MyViewController.m

    // MyViewController.m
    
    #import "MyViewController.h"
    
    @interface MyViewController ()
    
    @end
    
    @implementation MyViewController
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    }
    
    // This method will be overridden by myTableViewDataSource
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return 1;
    }
    
    - (int)myRequiredMethod {
        return 2;
    }
    

    Subclassing is a great way to make reusable custom UI elements.

提交回复
热议问题