0

我想在这张照片上做这样的事情: 在此处输入图像描述

我做模板显示节点:

@interface TemplateDisplayNode : ASDisplayNode

    @property (nonatomic, strong) ASDisplayNode *headerNode;
    @property (nonatomic, strong) ASDisplayNode *ContentNode;
    - (void)addContentNode:(ASDisplayNode *)contentNode;

    @end

    @implement TemplateDisplayNode

    - (instancetype)init
    {
        self = [super init];
        if (self) {
            [self createSubviews];
        }
        return self;
    }

    - (void)createSubviews {
        [self createHeaderNode];
        [self createContentNode];
    }

    - (void)addContentNode:(ASDisplayNode *)contentNode {
        [self.contentNode addSubnode:contentNode];
    }

    - (void)createHeaderNode {
        self.headerNode = [[ASDisplayNode alloc] init];
        self.headerNode.backgroundColor = [UIColor blueColor];
        [self addSubnode:self.headerNode];
    }

    - (void)createContentNode {
        self.contentNode = [[ASDisplayNode alloc] init];
        self.contentNode.style.flexGrow = YES;
        [self addSubnode:self.contentNode];
    }

    - (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize {

        CGFloat mainScreenWidth = [UIScreen mainScreen].bounds.size.width;

        NSMutableArray *mainStackContent = [[NSMutableArray alloc] init];

        if (self.headerNode) { 

            self.headerNode.style.preferredSize = CGSizeMake(mainScreenWidth, 48);
            [mainStackContent addObject:self.headerNode];
        }
        if (self.contentNode) {
            [mainStackContent addObject:self.contentNode];
        }
        ASStackLayoutSpec *contentSpec =
        [ASStackLayoutSpec
         stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical
         spacing:0
         justifyContent:ASStackLayoutJustifyContentStart
         alignItems:ASStackLayoutAlignItemsStretch
         children:mainStackContent];

        return contentSpec;
    }

    @end

并创建 TemplateDisplayNode 类的页面:

@interface Page1Node : TemplateDisplayNode

@end

@implement Page1Node

- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize {
    [super layoutSpecThatFits:constrainedSize];

}

@end

并在 ASViewController 中使用:

@interface MyViewController : ASViewController

@property (nonatomic, strong) MyCustomASViewControllerWithTableNode *myTableNodeViewController;
@property (nonatomic, strong) Page1Node *page1node;

@end

@implement MyViewController

- (instancetype)init {
    self.page1node = [[Page1Node alloc] init];
    self = [super initWithNode:self.page1node];

    if (self) {
        [self createMyTableNodeViewController];
    }

    return self;
}

- (void)createMyTableNodeViewController {
    self.myTableNodeViewController = [[MyCustomASViewControllerWithTableNode alloc] init];
    [self.page1node addContentNode:self.myTableNodeViewController.node]; //add TableNode like content on page1node
    [self addChildViewController:self.myTableNodeViewController];
    [self.myTableNodeViewController didMoveToParentViewController:self];
}

@end

MyCustomASViewControllerWithTableNode:

@interface MyCustomASViewControllerWithTableNode : ASViewController
@property (nonatomic, strong) ASTableNode *tableNode;
@end
...
- (instancetype)init {
    _tableNode = [[ASTableNode alloc] initWithStyle:UITableViewStylePlain];
    self = [super initWithNode:_tableNode];

    if (self) {
        _tableNode.dataSource = self;
        _tableNode.delegate = self;
    }

    return self;
}

当我运行它时,我在内容字段中看到一个表格,但我无法滚动表格,因为 Size frame ContentNode = {0; 0}; 如何正确进行布局以设置大小?如果我做同样的事情,但不使用 TemplateDisplayNode,那么一切正常。我想使用 TemplateDisplayNode 来删除类似页面的大量重复代码。不建议使用 AsyncDisplayKit 文档:

[super layoutSpecThatFits:constrainedSize];

如果删除此行,则不会进入 TemplateDisplayNode 中的 layoutSpecThatFits。建议怎么做?可以有哪些解决方案?

4

0 回答 0