简体   繁体   English

重用UITableView及其UI / delegate方法

[英]REUSE a UITableView with its UI/delegate methods

I have 2 UIViewControllers , the 2 ones are containing EXACTLY the SAME UITableView (with its custom cells and delegate methods). 我有2个UIViewControllers ,其中2个完全包含相同的UITableView (及其自定义单元格和委托方法)。 My question is their any way to "centralize" the UITableView UI and code(datasource and delegates), so that I just have to modify in one file instead of 2 . 我的问题是他们可以“集中” UITableView UI和代码(数据源和委托)的任何方式,因此我只需要在一个文件而不是2中进行修改。

following up on my comment, the table view in the xib in your father vc and the delegate methods in your father vc are just in the same place because you chose it to be like that, the table view and the delegate methods are actually quite detached. 根据我的评论,父亲vc的xib中的表视图和父亲vc中的委托方法位于同一位置,因为您选择的是这样,表视图和委托方法实际上是非常分离的。

so create a new object, say FatherTableController which implements UITableViewDatasource and UITabelViewDelegate and copy those methods out of your FatherViewController into this FatherTableController 所以创建一个新的对象,说FatherTableController它实现UITableViewDatasourceUITabelViewDelegate和这些方法从你的复制FatherViewController到这个FatherTableController

now in your FatherViewController, go like 现在在您的FatherViewController中,像

FatherTableController tableController = [FatherTableController new]; //should be a property or a singleton

self.tableview.delegate = tableController;
self.tableview.datasource = tableController;

now you can do that in both your separate vc's that use the same table, and even use the exact same table contoller between the two views if you share it in some way (possibly via a singleton pattern, which can be useful for sharing state between the two view controllers) 现在,您可以在使用同一表的两个单独的vc中做到这一点,如果以某种方式共享它(甚至通过单例模式,这对于在两个视图之间共享状态也很有用),甚至可以在两个视图之间使用完全相同的表contoller两个视图控制器)

Solution: 解:

@interface FatherViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UITableView *parentTableView;

@implementation FatherViewController

    - (void)viewDidLoad {
        [super viewDidLoad];

        self.parentTableView.delegate=self;
        self.parentTableView.dataSource=self;
    }

    //declare the delegate / datasource methods

--------------------- CHILD VIEW CONTROLLER --------------------- ---------------------儿童视图控制器---------------------

@interface ViewController : FatherViewController 

@property (strong, nonatomic) IBOutlet UITableView *tableView;

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.delegate=self;
    self.tableView.dataSource=self;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        return [super tableView:tableView cellForRowAtIndexPath:indexPath];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [super numberOfSectionsInTableView:tableView];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [super tableView:tableView numberOfRowsInSection:section];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM