#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);
}];