UITableView或者UICollecView中相互嵌套并且自适应方法

#import "JHVehicleBaoYangInfoSelectListTableViewCell.h"

#import "JHVehicleBaoYangInfoSelectListCollectionViewCell.h"

 

@interface JHVehicleBaoYangInfoSelectListTableViewCell () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

 

@property (nonatomic, weak) IBOutlet UICollectionView *collectionView;

 

@end

 

@implementation JHVehicleBaoYangInfoSelectListTableViewCell

 

//- (void)setResource:(NSArray *)resource {

//    if (kCheckRObject(resource)) {

//        return;

//    }

//    _resource = resource;

//

//    for (int i=0; i<self.resource.count; i++) {

//

//    }

//}

 

- (void)awakeFromNib {

    [super awakeFromNib];

    // Initialization code

    

    self.resource = @[@"测试信息测试信息", @"测试", @"测试信息测试信息", @"测试", @"测试信息测试信息", @"测试信息测试信息", @"测试", @"测试信息测试信息", @"测试信息测试信息", @"测试信息测试信息", @"测试信息测试信息", @"测试信息测试信息", @"测试"@"测试信息测试信息", @"测试", @"测试信息测试信息", @"测试", @"测试信息测试信息", @"测试信息测试信息", @"测试", @"测试信息测试信息", @"测试信息测试信息", @"测试信息测试信息", @"测试信息测试信息", @"测试信息测试信息", @"测试"];

    

    [self createUI];

}

 

- (void)createUI {

    CGFloat both = 10.f;

    CGFloat base = 13.f;

    

    CGFloat width = (KScreenWidth - both*2 - base*2)/3;

    CGFloat height = width * 179.f / 218.f;

    

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];

    [self.collectionView setCollectionViewLayout:layout];

    

    layout.minimumLineSpacing = base;

    layout.minimumInteritemSpacing = base;

    layout.sectionInset = UIEdgeInsetsMake(base, 10, base, 10);

    

    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([JHVehicleBaoYangInfoSelectListCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:@"JHVehicleBaoYangInfoSelectListCollectionViewCell"];

    

    self.collectionView.dataSource = self;

    self.collectionView.delegate = self;

    

    //在可以获得tableView准确属性的任何地方都可以调用此方法将tableView滚动到底部

    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:(self.resource.count-1) inSection:0] atScrollPosition:UICollectionViewScrollPositionBottom animated:false];

    [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {

        make.height.mas_equalTo(self.collectionView.collectionViewLayout.collectionViewContentSize.height);

    }];

}

 

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return 1;

}

 

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return self.resource.count;

}

 

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    NSString *content = self.resource[indexPath.row];

    CGSize labelSize = [UILabel labelConstrainedHeightByWidth:(KScreenWidth-20) content:content font:[UIFont fontByName:nil fontSize:15]];

    return labelSize;

}

 

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    JHVehicleBaoYangInfoSelectListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"JHVehicleBaoYangInfoSelectListCollectionViewCell" forIndexPath:indexPath];

    [cell.contentButton setTitle:self.resource[indexPath.row] forState:UIControlStateNormal];

    return cell;

}

 

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    

}

 

 

@end

 

 

 

说明:核心代码是

[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:(self.resource.count-1) inSection:0] atScrollPosition:UICollectionViewScrollPositionBottom animated:false];

    [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {

        make.height.mas_equalTo(self.collectionView.collectionViewLayout.collectionViewContentSize.height);

    }];

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nhfc99

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值