美文网首页
UIStackView_TG

UIStackView_TG

作者: 唐梦泽 | 来源:发表于2021-10-25 00:03 被阅读0次

#import "TGUIStackViewController.h"

#import "TGStackCell.h"

#import "TGStackItem.h"

@interface TGUIStackViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) UITableView *tableView;

@end

@implementation TGUIStackViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    UITableView*tableView =UITableView.new;

    [self.viewaddSubview:tableView];

    [tableViewmas_makeConstraints:^(MASConstraintMaker*make) {

        make.edges.equalTo(self.view);

    }];

    self.tableView= tableView;

    tableView.dataSource=self;

    tableView.delegate=self;

}

- (NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section {

    return3;

}

- (UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath {

    TGStackCell*cell = [TGStackCellcellForTableView:tableView];

    cell.items = @[TGStackItem.new, TGStackItem.new, TGStackItem.new, TGStackItem.new];

    returncell;

}

- (CGFloat)tableView:(UITableView*)tableViewheightForRowAtIndexPath:(NSIndexPath*)indexPath {

    return100;

}

@end

@class TGStackItem;

@interface TGStackCell : UITableViewCell

@property (nonatomic, strong) NSArray<TGStackItem *>*items;

+ (instancetype)cellForTableView:(UITableView*)tableView;

@end

#import "TGStackCell.h"

#import "TGStackCellInnerView.h"

@interface TGStackCell ()

@property (strong, nonatomic) UIStackView *stackV;

@end

@implementation TGStackCell

- (void)setItems:(NSArray *)items {

    [self.stackV.arrangedSubviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

    _items= items;

    for(inti =0; i < items.count; i++) {

        TGStackCellInnerView *v = [TGStackCellInnerView stackInnerViewWithItem:items[i]];

        [self.stackV addArrangedSubview:v];

        [vmas_makeConstraints:^(MASConstraintMaker*make) {

            make.width.equalTo(@100);

        }];

    }

}

+ (instancetype)cellForTableView:(UITableView*)tableView{

    TGStackCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([self class])];

    if(!cell) {

        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([self class])];

    }

    returncell;

}

- (instancetype)initWithStyle:(UITableViewCellStyle)stylereuseIdentifier:(NSString*)reuseIdentifier{

    if(self=[superinitWithStyle:stylereuseIdentifier:reuseIdentifier]) {

        [selfUIInit];

    }

    return self;

}

- (void)UIInit{

    self.contentView.backgroundColor = [UIColor whiteColor];

    self.selectionStyle = UITableViewCellSelectionStyleNone;

    UIScrollView*outScrollV = [[UIScrollViewalloc]init];

    outScrollV.showsHorizontalScrollIndicator = NO;

    outScrollV.showsVerticalScrollIndicator = NO;

    [self.contentView addSubview:outScrollV];

//    outScrollV.backgroundColor = UIColor.yellowColor;

    [outScrollVmas_makeConstraints:^(MASConstraintMaker*make) {

        make.edges.mas_equalTo(UIEdgeInsetsMake(12,12,12,12));

    }];

    [outScrollVaddSubview:self.stackV];

    [self.stackV mas_makeConstraints:^(MASConstraintMaker *make) {

        make.edges.equalTo(outScrollV);

        make.height.equalTo(outScrollV);

    }];

}

- (UIStackView *)stackV {

    if(!_stackV) {

        UIStackView*stackV = [[UIStackViewalloc]init];

//        stackV.backgroundColor = UIColor.redColor;

        stackV.axis = UILayoutConstraintAxisHorizontal;

        stackV.spacing=12;

        stackV.alignment = UIStackViewAlignmentFill;

        stackV.distribution = UIStackViewDistributionFill;

        _stackV= stackV;

    }

    return _stackV;

}

@end

@class TGStackItem;

@interface TGStackCellInnerView : UIView

+ (instancetype)stackInnerViewWithItem:(TGStackItem *)item;

@end

#import "TGStackCellInnerView.h"

#import "TGStackItem.h"

@interface TGStackCellInnerView ()

@property (strong, nonatomic) UIImageView *backImageV;

@property (strong, nonatomic) UILabel *titleL;

@property (strong, nonatomic) UILabel *playCountL;

@property (strong, nonatomic) TGStackItem *item;

@end

@implementation TGStackCellInnerView

- (instancetype)initWithFrame:(CGRect)frame {

    if(self=[superinitWithFrame:frame]) {

        [selfInitUI];

    }

    return self;

}

- (void)InitUI{

    [selfaddSubview:self.titleL];

    [self.titleL mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.right.bottom.equalTo(self);

    }];

    self.titleL.text=@"999";

    UIView*topContainer = [[UIViewalloc]init];

    topContainer.backgroundColor = UIColor.orangeColor;

    [selfaddSubview:topContainer];

    [topContainermas_makeConstraints:^(MASConstraintMaker*make) {

        make.top.left.right.equalTo(self);

        make.bottom.equalTo(self.titleL.mas_top).offset(-5);

    }];

    [topContaineraddSubview:self.backImageV];

    [self.backImageV mas_makeConstraints:^(MASConstraintMaker *make) {

        make.edges.equalTo(topContainer);

    }];

}

+ (instancetype)stackInnerViewWithItem:(TGStackItem *)item {

    TGStackCellInnerView *v = [[TGStackCellInnerView alloc] init];

    v.item= item;

    returnv;

}

- (UIImageView *)backImageV {

    if(!_backImageV) {

        _backImageV = UIImageView.new;

    }

    return _backImageV;

}

// 播放量

- (UILabel *)playCountL{

    if (!_playCountL) {

        _playCountL=UILabel.new;

        _playCountL.font = [UIFont systemFontOfSize:14];

        _playCountL.textColor = [UIColor orangeColor];

    }

    return _playCountL;

}

// 标题

- (UILabel*)titleL{

    if(!_titleL) {

        _titleL=UILabel.new;

        _titleL.font = [UIFont systemFontOfSize:16];

        _titleL.textColor = [UIColor blackColor];

    }

    return _titleL;

}

- (void)setItem:(TGStackItem*)item {

    _item= item;

}

@end

相关文章

  • UIStackView_TG

    #import "TGUIStackViewController.h" #import "TGStackCell....

网友评论

      本文标题:UIStackView_TG

      本文链接:https://www.haomeiwen.com/subject/dbbgdftx.html