UICollectionView、UItableView动态改变⾼度
在之前做项⽬的时候,机会性的将UICollectionView 、UItableView 放在了⼀个UI视图中展⽰,并且需要动态布局的那种,在以下代码中,也可以动态计算collection的⾼度
以下是⼩⼋⾃⼰简单敲得代码,共读者参考,有任何意见或者建议,都欢迎各位留⾔,发邮件也可以的哟,⼩⼋在此先谢过各位读者啦!^-^  ^-^
话不多说,直接上代码了哟
//ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (nonatomic, strong) UITableView      *autoTableView;
@property (nonatomic, strong) UICollectionView * headerView;
@end
//  ViewController.m
#import "ViewController.h"
#import "CollectionViewCellItems.h"
#define SCREEN_WIDTH  ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT  ([UIScreen mainScreen].bounds.size.height)
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate>
{
NSMutableArray *collectionViewResource;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 20)];
[rightBtn setTitle:@"changed" forState:UIControlStateNormal];
[rightBtn addTarget:self action:@selector(changCollectionLayout) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
self.navigationItem.rightBarButtonItem = rightButton;
self.view.backgroundColor = [UIColor greenColor];
collectionViewResource = [NSMutableArray array];
[self.view addSubview:[UIView new]];//⽤来解决tableview的grouped样式时,顶部间距问题
[self.view addSubview:self.autoTableView];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self remakeDataSource];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSInteger rows;
switch (section) {
case 0:
rows = 1;
break;
case 1:
rows = 2;
break;
case 2:
rows = 3;
break;
default:
break;
}
return rows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"RCDGroupSettingsTableViewCell";
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
< = @"textLabel";
= @"detailTextLabel";
return cell;
}
#pragma mark - Table view data delegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if (section == 0) {
return 0.01f;
}
return 14.f;
}
-
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 44.f;
}
#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(55, 55);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSIn    return 8;
return 8;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)sectio    UICollectionViewFlowLayout *flowLayout =(UICollectionViewFlowLayout *)collectionViewLayout;
flowLayout.minimumInteritemSpacing = 0;
float index=(SCREEN_WIDTH-4*61)/10.;
if (unt >0) {
return UIEdgeInsetsMake(10, 10, 10, index);
}
return UIEdgeInsetsMake(0, 10, 0, index);
}
#pragma mark - Collection view data source
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
unt;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCellItems *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[CollectionViewCellItems CollectionCellIdentifier] forIndexPath:indexPa    NSDictionary *indexDict = w];
= [NSString stringWithFormat:@"%li",[indexDict[@"ItemsIndexPath"] integerValue]];
return cell;
}
#pragma mark - Collection view delegate
- (void)changCollectionLayout {
NSLog(@"chaanged");
[self remakeDataSource];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - ⾃定义私有⽅法
- (void)remakeDataSource {
NSInteger itemsCount = random()%30;
[collectionViewResource removeAllObjects];
for (int i = 0; i < itemsCount; i ++) {
NSDictionary *itemsDict = @{@"ItemsIndexPath":@(i)};
[collectionViewResource addObject:itemsDict];
}
[_headerView reloadData];
[_autoTableView reloadData];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
_headerView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width,_llectionViewContentSize.height);
self.autoTableView.tableHeaderView = _headerView;
});
}
#pragma mark - 初始化控件信息
#pragma mark - 初始化控件信息
- (UITableView *)autoTableView {
if (!_autoTableView) {
CGFloat pointY = [UIApplication sharedApplication].statusBarFrame.size.height + self.navigationContr
oller.navigationBar.frame.size.height;
_autoTableView = [[UITableView alloc]initWithFrame:CGRectMake(0,pointY, SCREEN_WIDTH, SCREEN_HEIGHT- pointY) style:UITableViewStyleGrouped];        _autoTableView.dataSource = self;
_autoTableView.delegate = self;
_autoTableView.sectionFooterHeight = 1.0;
_autoTableView.tableFooterView = [UIView new];
_autoTableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
if ([_autoTableView respondsToSelector:@selector(setSeparatorInset:)]) {
[_autoTableView setSeparatorInset:UIEdgeInsetsMake(0, 10, 0, 0)];
}
_autoTableView.tableHeaderView = self.headerView;
}
return _autoTableView;
}
- (UICollectionView *)headerView {
if (!_headerView) {
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
_headerView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200) collectionViewLayout:flowLayout];
_headerView.delegate = self;
_headerView.dataSource = self;
_headerView.scrollEnabled = NO;
_headerView.backgroundColor = [UIColor whiteColor];
[_headerView registerClass:[CollectionViewCellItems class] forCellWithReuseIdentifier:[CollectionViewCellItems CollectionCellIdentifier]];
}
return _headerView;
}
@end
//  CollectionViewCellItems.h
#import <UIKit/UIKit.h>
@interface CollectionViewCellItems : UICollectionViewCell
@property (nonatomic, strong) UILabel  *itemsLabel;
+ (NSString*)CollectionCellIdentifier;
@end
//  CollectionViewCellItems.m
#import "CollectionViewCellItems.h"
@implementation CollectionViewCellItems
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
#pragma mark ##1  基本信息
#pragma mark ##2 添加控件到当前
view ui框架
[tView addSubview:self.itemsLabel];
#pragma mark ##3 添加约束
}
return self;
}
- (UILabel *)itemsLabel  {
if (!_itemsLabel) {
_itemsLabel = [[UILabel alloc]tView.frame];
_Color = [UIColor blackColor];
_Alignment = NSTextAlignmentCenter;
}
return _itemsLabel;
}
+ (NSString*)CollectionCellIdentifier {
return @"CollectionViewCellItemsIdentifier";
}
@end
以上代码仅供各位读者参考,有任何想法或者建议啥的,⼩⼋都⾮常欢迎各位⽼师留⾔! ^-^  ^-^