UICollectionView 、UItableView 动态改变高度

来源:互联网 发布:数学出题软件 编辑:程序博客网 时间:2024/05/18 14:46

在之前做项目的时候,机会性的将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;    }    cell.textLabel.text = @"textLabel";    cell.detailTextLabel.text = @"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:(NSInteger)section {    return 8;}- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {    UICollectionViewFlowLayout *flowLayout =(UICollectionViewFlowLayout *)collectionViewLayout;    flowLayout.minimumInteritemSpacing = 0;    float index=(SCREEN_WIDTH-4*61)/10.;    if (collectionViewResource.count >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 {        return collectionViewResource.count;}- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {        CollectionViewCellItems *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[CollectionViewCellItems CollectionCellIdentifier] forIndexPath:indexPath];    NSDictionary *indexDict = collectionViewResource[indexPath.row];    cell.itemsLabel.text = [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,_headerView.collectionViewLayout.collectionViewContentSize.height);        self.autoTableView.tableHeaderView = _headerView;    });}#pragma mark - 初始化控件信息- (UITableView *)autoTableView {        if (!_autoTableView) {        CGFloat pointY = [UIApplication sharedApplication].statusBarFrame.size.height + self.navigationController.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  基本信息        self.contentView.backgroundColor = [UIColor greenColor];#pragma mark ##2 添加控件到当前                [self.contentView addSubview:self.itemsLabel];    #pragma mark ##3 添加约束    }    return self;}- (UILabel *)itemsLabel  {    if (!_itemsLabel) {        _itemsLabel = [[UILabel alloc]initWithFrame:self.contentView.frame];        _itemsLabel.textColor = [UIColor blackColor];        _itemsLabel.textAlignment = NSTextAlignmentCenter;    }    return _itemsLabel;}+ (NSString*)CollectionCellIdentifier {        return @"CollectionViewCellItemsIdentifier";}@end


以上代码仅供各位读者参考,有任何想法或者建议啥的,小八都非常欢迎各位老师留言! ^-^   ^-^ 
0 0
原创粉丝点击