对象-扩展-TableView委托

问题描述

我正在尝试在一个小型objc项目中遵循Viper模式。我得到了每个部分的不同角色,没有特别的问题。 但是,我遇到的问题是当我尝试将表视图的委托/数据源移动到另一个文件时,因为我读到这是应该完成的。 我关注了该帖子:iOS using VIPER with UITableView,但我无法进行编译。

这里的问题是我不知道如何在Objc中进行扩展。我尝试了很多语法,但是没有一个起作用。 如果你们可以使用“ MyViewController.m / h”和“ MyTableViewController.m / h”做一个简单的例子,其中“ MyTableViewController”是“ MyViewController”的扩展。 这意味着我们将在“ MyViewController.h”中看到<UITableViewDelegate>

非常感谢您的帮助。这可能是一个多余的问题,但是我没有找到关于扩展名问题的明确答案。

解决方法

感谢@ Kamil.S在上面的评论中,我设法在Apple文档中找到了想要的东西! 实际上,Objc中的扩展名称为“类别”。我几乎做了我在原始问题中链接的帖子中写的内容。

因此,如果有人需要,这是一个简化的示例:

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (strong,nonatomic) id<ViewToPresenterProtocol> presenter;
@end

ViewController.m

#import "ViewController.h"

@implementation ViewController
// All my code,ViewDidLoad,and so on
@end

CollectionViewController.h

#import <UIKit/UIKit.h>
#import "ViewController.h"

@interface ViewController (CollectionViewController) <UICollectionViewDelegate,UICollectionViewDataSource>
@end

CollectionViewController.m

#import <UIKit/UIKit.h>
#import "CollectionViewController.h"
#import "ViewController.h"

@implementation ViewController (CollectionViewController)

- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [self.presenter getNumberOfItems];
}

// ...
// Here add others functions for CollectionView Delegate/Datasource protocols

@end