效果展示
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
#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; //顯示標題數(shù)組
@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布局
#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