我怎样才能inheritanceUITableView?

我想创build一个UITableView的子类,因为我想在我的应用程序中创build一个可重用的表视图组件。

这个想法是不是使用委托说cellForRowAtIndexPath我希望表视图本身来获得该调用。

我不认为我想要一个UITableViewController作为这个UITableView我想要build立必须住在各种UIViewControllers(和这些UIViewController可能有自己的UITableViews)。

我将我的UITableView分类为:

@interface ShareUITableView : UITableView 

但没有一个方法被调用。

我的ShareUITableView是通过将自定义类设置为ShareUITableView通过NIB创build的。 我在代码中validation了一个ShareUITableView被实例化。

我的UITableView不委托给它的视图控制器,所以这不是问题。

有任何想法吗?

我想,你仍然应该去一个控制器类。 我期望UITableView的inheritance是乏味的工作 – 如果可能的话,合理的金额。

让UIViewController / NoViewController实现委托和数据源,并将另一个控制器分配给特定的tableView没有问题。 请注意,数据源和委托不需要是UITableViewController的子类。

看看这个答案: 在运行时实现委托?

我的UITableView不委托给它的视图控制器,所以这不是问题。

你必须使用委托和数据源,这是如何填充和configurationTableViews。 否则你将不得不覆盖UITableView的所有方法 – 包括私有方法,如果你想进入AppStore则不要去。 重新创buildUITableView而不创build子类会更容易。

如果我理解你,你需要这个类的声明:

 @interface ShareUITableView : UITableView <UITableViewDataSource> 

然后,在您的类构造函数中,您应该将实例本身分配为自己的数据源:

 - (id)init { //... self.dataSource = self; //... } 

当然,class级将不得不采用协议。

祝你好运!

MyTableView.h

 // MyTableView.h // This overrides the UITableViewDataSource with your own so you can add any methods you would like. @protocol MyTableViewDataSource <UITableViewDataSource> @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 <UITableViewDelegate> @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 <UITableViewDelegate, UITableViewDataSource> { // Your customer datasource and delegate. id <MyTableViewDataSource> myDataSource; id <MyTableViewDelegate> 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<MyTableViewDataSource>)self.dataSource; myDelegate = (id<MyTableViewDelegate>)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 <MyTableViewDataSource, MyTableViewDelegate> { @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; } 

子类化是制作可重用的自定义用户界面元素的好方法。