美文网首页
UITableView中加UICollectonView

UITableView中加UICollectonView

作者: 陈胜华 | 来源:发表于2017-06-30 17:49 被阅读38次

    效果展示

    效果图.png

    1 .将TableView初始化在ViewController上

    #import "ViewController.h"
    #import "TableViewCell.h"
    
    @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
    
    @property (nonatomic,strong) UITableView *tabeView;
    
    @end
    
    @implementation ViewController
    
    - (UITableView *)tabeView {
        
        if (_tabeView == nil) {
            _tabeView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)) style:UITableViewStylePlain];
            _tabeView.delegate = self;
            _tabeView.dataSource = self;
            _tabeView.estimatedRowHeight = 200;
            _tabeView.rowHeight = UITableViewAutomaticDimension;
        }
        
        return _tabeView;
    }
    
    - (void)viewDidLoad {
        
        [super viewDidLoad];
    
        [self.view addSubview:self.tabeView];
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        
        return 10;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        NSArray *titles = @[
                            @"出售",@"委托",@"约看",@"清单",@"列表",
                            @"出售",@"委托",@"约看",@"清单",@"列表",
                            @"出售",@"委托",@"约看",@"清单",@"列表",
                            @"出售",@"委托",@"约看",@"清单",@"列表",
                            ];
        
        return [TableViewCell initTableViewCell:tableView titleArrays:titles];
    }
    
    @end
    

    2.TableViewCell中添加UICollectionView 和UIPageControl


    TableView.xib结构
    #import "TableViewCell.h"
    #import "CollectionViewCell.h"
    
    @interface TableViewCell()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
    
    @property (weak,  nonatomic) IBOutlet UICollectionView *collectionView;
    
    @property (weak, nonatomic) IBOutlet UIPageControl *pageControl;      //page显示
    
    @property (strong,nonatomic) UICollectionViewFlowLayout *flowLayout;  //UICollectionFlowLayout布局
    
    @property (strong,nonatomic) NSArray *titlesArray;                    //显示标题数组
    
    @end
    
    static NSString *collectionIdentifier = @"CollectionViewCell";
    
    @implementation TableViewCell
    
    + (TableViewCell *)initTableViewCell:(UITableView *)tableView titleArrays:(NSArray*)titiles {
    
        TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([TableViewCell class])];
        
        if (cell == nil) {
            
            cell = [[[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil] lastObject];
        }
        
        cell.titlesArray = titiles;
        cell.pageControl.numberOfPages = titiles.count/5 + 1;
        cell.flowLayout = [[UICollectionViewFlowLayout alloc]init];
        cell.flowLayout.itemSize = CGSizeMake(([UIScreen mainScreen].bounds.size.width-16)/4, 100);
        cell.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        [cell.collectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:collectionIdentifier];
        cell.collectionView.collectionViewLayout = cell.flowLayout;
        cell.collectionView.pagingEnabled = YES;
        cell.collectionView.delegate = cell;
        cell.collectionView.dataSource = cell;
        return cell;
    }
    
    -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
        
        return 1;
    }
    
    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
        return self.titlesArray.count;
    }
    
    - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
        CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionIdentifier forIndexPath:indexPath];
    
        [cell setModel:self.titlesArray[indexPath.item] item:indexPath.item];
        
        return cell;
    }
    
    //UICollectionViewFlowLayout 布局
    -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
        
        return CGSizeMake(([UIScreen mainScreen].bounds.size.width-16)/4, 100);
    }
    
    -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
    {
        
        return UIEdgeInsetsMake(0, 0, 0, 0);
        
    }
    
    -(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
        
        return 0;
    }
    
    -(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
        
        return 0;
    }
    
    -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
        NSInteger curPageIndex = scrollView.contentOffset.x / ([UIScreen mainScreen].bounds.size.width - 16);
        
        //滑动,显示第几页
        self.pageControl.currentPage = curPageIndex;
    }
    
    
    - (void)awakeFromNib {
        [super awakeFromNib];
    }
    
    - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
        [super setSelected:selected animated:animated];
    }
    
    @end
    

    3.CollectionViewCell布局

    CollectionViewCell.xib布局
    
    #import "CollectionViewCell.h"
    
    @interface CollectionViewCell()
    
    @property (weak, nonatomic) IBOutlet UILabel *elblDesc;
    
    @end
    
    @implementation CollectionViewCell
    
    - (void)setModel:(NSString *)title item:(NSInteger)indexRow {
        
        if (indexRow % 5 == 0) {
            
            self.backgroundColor  = [UIColor yellowColor];
        } else if (indexRow % 5 == 1){
        
            self.backgroundColor  = [UIColor lightGrayColor];
        }else if (indexRow % 5 == 2){
            
            self.backgroundColor  = [UIColor redColor];
        }else if (indexRow % 5 == 3){
            
            self.backgroundColor  = [UIColor greenColor];
        }else if (indexRow % 5 == 4){
            
            self.backgroundColor  = [UIColor darkGrayColor];
        }
        
        self.elblDesc.text = title?:@"";
    }
    
    
    - (void)awakeFromNib {
        [super awakeFromNib];
    }
    
    @end
    

    相关文章

      网友评论

          本文标题:UITableView中加UICollectonView

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