ASDK学习demo

ASViewController 初始化 ASTableNode

@interface First : ASViewController
//<ASTableNode *>
@end


#import "First.h"
#import "ZYCellNode.h"

@interface First ()<ASTableDelegate, ASTableDataSource>
{
    ASTableNode *_table;
}
@end

@implementation First
- (instancetype)init
{
    ASDisplayNode *node = [ASDisplayNode new];
    node.backgroundColor = [UIColor whiteColor];
    _table = [ASTableNode new];
    [node addSubnode:_table];
    
    self = [super initWithNode:node];
    if (self) {
        _table.delegate = self;
        _table.dataSource = self;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

// table view frame 根据父视图变化 手机转动时候会调用
- (void)viewWillLayoutSubviews {
    _table.frame = self.node.bounds;
}

- (NSInteger)tableNode:(ASTableNode *)tableNode numberOfRowsInSection:(NSInteger)section {
    return 100;
}

- (ASCellNode *)tableNode:(ASTableNode *)tableNode nodeForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZYCellNode *node = [[ZYCellNode alloc] init];
    NSAttributedString *att = [[NSAttributedString alloc] initWithString:@"123" attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:15], NSForegroundColorAttributeName: [UIColor blackColor]}];
    NSAttributedString *att2 = [[NSAttributedString alloc] initWithString:@"123" attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:15], NSForegroundColorAttributeName: [UIColor grayColor]}];
    node.titleNode.attributedText = att;
    node.contentNode.attributedText = att2;
    return node;
}

- (void)tableNode:(ASTableNode *)tableNode didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end

ASCellNode 子类

@interface ZYCellNode : ASCellNode
@property(nonatomic, strong)ASTextNode *titleNode;
@property(nonatomic, strong)ASTextNode *contentNode;
@end

#import "ZYCellNode.h"

@implementation ZYCellNode
- (instancetype)init {
    self = [super init];
    if (self) {
        _titleNode = [[ASTextNode alloc] init];
        _contentNode = [[ASTextNode alloc] init];
        [self addSubnode:_titleNode];
        [self addSubnode:_contentNode];
    }
    return self;
}

- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize {
    ASStackLayoutSpec *stack = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical
                                                                       spacing:10
                                                                justifyContent:ASStackLayoutJustifyContentStart
                                                                    alignItems:ASStackLayoutAlignItemsStart
                                                                      children:@[_titleNode, _contentNode]];
    return [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(10, 10, 10, 10) child:stack];
}

@end

感觉ASDK挺好用的,和iOS原生代码布局思想不同

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容