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原生代码布局思想不同
网友评论