天天看點

實作collectionview加載到tableview的tableFooterView上一起滑動(類似淘寶購物車)--通過添加觀察者實作

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"交易成功";
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.leftBarButtonItem = [BackButtonItem getBackItemWithTitle:@""  image:[UIImage imageNamed:@"fl_fanhui-jian"] target:self action:@selector(goBackPage:)];
    
    //右側确認按鈕
    self.rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.rightBtn.userInteractionEnabled = YES;
    [self.rightBtn setImage:[UIImage imageNamed:@"gengduoanniu"] forState:UIControlStateNormal];
    self.rightBtn.titleLabel.font = MIDDLE_LIT_FONT;
    [self.rightBtn setTitleColor:THEME_GREEN_COLOR forState:UIControlStateNormal];
    [self.rightBtn addTarget:self action:@selector(clickedRightBtn:) forControlEvents:UIControlEventTouchUpInside];
    self.rightBtn.frame = CGRectMake(0, 0, 40, 30) ;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.rightBtn];
    
    
    [self drawMainViewPage];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.translucent = YES;
    [self.myColView reloadData];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

-(void)drawMainViewPage{
    self.myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH) style:UITableViewStylePlain];
    [self.view addSubview:self.myTableView];
    [self.myTableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top);
        make.left.right.bottom.mas_equalTo(self.view);
    }];
    self.myTableView.backgroundColor = BKG_HEX_GRAY_EEE;
    [self.myTableView layoutIfNeeded];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.estimatedRowHeight = 49;
    self.myTableView.rowHeight = UITableViewAutomaticDimension;
    [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //
    [self drawHeaderView];
    [self drawFooterCol];
}

-(void)drawHeaderView{
    self.headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,190)];
    self.myTableView.tableHeaderView = self.headerView;
    self.headerView.backgroundColor = [UIColor whiteColor];
    //上
    self.headerImgv = [[UIImageView alloc]init];
    self.headerImgv.backgroundColor = [UIColor whiteColor];
    self.headerImgv.image = [UIImage imageNamed:@"jiaoyiwanchengtupian"];
    [self.headerView addSubview:self.headerImgv];
    [self.headerImgv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.headerView.mas_top);
        make.left.right.mas_equalTo(self.headerView);
        make.height.mas_equalTo(SCREEN_WIDTH*(124/375.0));
    }];
    [self.headerImgv layoutIfNeeded];
    
    CGFloat widht_btn = (SCREEN_WIDTH-40*2- 18)/2.0;
    
    self.liJiPJBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.headerView addSubview:self.liJiPJBtn];
    self.liJiPJBtn.layer.borderColor = TEXT_HEX_MID_GRAY.CGColor;
    self.liJiPJBtn.layer.borderWidth = 1;
    self.liJiPJBtn.layer.cornerRadius = 20/2.0;
    self.liJiPJBtn.titleLabel.font = BIG_B_FONT;
    [self.liJiPJBtn setTitle:@"立即評價" forState:UIControlStateNormal];
    [self.liJiPJBtn setTitleColor:TEXT_HEX_LIGHT_L_GRAY forState:UIControlStateNormal];
    [self.liJiPJBtn addTarget:self action:@selector(clickedLiJIPIngJiaBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.liJiPJBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.headerView.mas_left).mas_offset(40);
        make.top.mas_equalTo(self.headerImgv.mas_bottom).mas_offset(22);
        make.size.mas_equalTo(CGSizeMake(widht_btn, 40));
    }];
    
    self.dingDxqBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.headerView addSubview:self.dingDxqBtn];
    self.dingDxqBtn.layer.borderColor = TEXT_HEX_MID_GRAY.CGColor;
    self.dingDxqBtn.layer.borderWidth = 1;
    self.dingDxqBtn.layer.cornerRadius = 20/2.0;
    self.dingDxqBtn.titleLabel.font = BIG_B_FONT;
    [self.dingDxqBtn setTitle:@"訂單詳情" forState:UIControlStateNormal];
    [self.dingDxqBtn setTitleColor:TEXT_HEX_LIGHT_L_GRAY forState:UIControlStateNormal];
    [self.dingDxqBtn addTarget:self action:@selector(clickedDingDanXiangQingaBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.dingDxqBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.liJiPJBtn.mas_right).mas_offset(18);
        make.top.mas_equalTo(self.headerImgv.mas_bottom).mas_offset(22);
        make.size.mas_equalTo(CGSizeMake(widht_btn, 40));
    }];
    
    self.headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH,self.headerImgv.frame.size.height+22+40+22);

}

-(void)drawFooterCol{
    self.footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 90)];
    self.myTableView.tableFooterView = self.footView;
    self.footView.backgroundColor = [UIColor clearColor];
    
    [self drawTitleCollection];
    [self drawColPage];
}
-(void)drawColPage{
    //284/376
    CGFloat cellWidth = (SCREEN_WIDTH-20-10)/2.0;
    CGFloat cellHeight = cellWidth*(188/167.0);
    
    self.flowLayout = [[UICollectionViewFlowLayout alloc]init];
    self.flowLayout.itemSize = CGSizeMake(cellWidth, cellHeight+10);
    self.flowLayout.minimumLineSpacing = 10;
    self.flowLayout.minimumInteritemSpacing = 10;
    self.flowLayout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    self.flowLayout.footerReferenceSize = CGSizeMake(self.myTableView.bounds.size.width, 0.0001);
    self.flowLayout.headerReferenceSize = CGSizeMake(self.myTableView.bounds.size.width, 0.0001);
    self.myColView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH) collectionViewLayout:self.flowLayout];
    self.myColView.delegate = self;
    self.myColView.dataSource = self;
    self.myColView.bounces = YES;
    self.myColView.scrollEnabled = NO;
    self.myColView.backgroundColor = [UIColor whiteColor];
    self.myColView.showsHorizontalScrollIndicator = NO;
    self.myColView.showsVerticalScrollIndicator = NO;
    [self.footView addSubview:self.myColView];
    [self.myColView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.labelTL.mas_bottom).mas_offset(TEXT_EDGE_DISTANCE);
        make.left.right.mas_equalTo(self.footView);
        make.height.mas_equalTo(100);
    }];
    
    [self.myColView registerClass:[SearchGoodsColCell class] forCellWithReuseIdentifier:@"SearchGoodsColCell_order"];
    
    [self.myColView layoutIfNeeded];
    
    
    [self.myColView addObserver:self forKeyPath:@"contentSize" options:0 context:NULL];
    
}
//監聽contentSize變化改變限制
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    
    CGRect frame = self.myColView.frame;
    frame.size = self.myColView.contentSize;
    self.footView.frame = CGRectMake(0, 0, SCREEN_WIDTH, frame.size.height);
    [self.footView layoutIfNeeded];
    self.myTableView.tableFooterView = self.footView;
    [self.myColView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.labelTL.mas_bottom).mas_offset(TEXT_EDGE_DISTANCE);
        make.left.right.mas_equalTo(self.footView);
        make.height.mas_equalTo(frame.size.height);
    }];
}
-(void)drawTitleCollection{
    //你喜歡
    self.labelTL = [[UILabel alloc]init];
    [self.footView addSubview:self.labelTL];
    self.labelTL.font = MIDDLE_LIT_FONT;
    self.labelTL.textColor = RGB(141, 139, 140);
    self.labelTL.text = @"猜你喜歡";
    [self.labelTL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.footView.mas_centerX);
        make.top.mas_equalTo(self.footView.mas_top).mas_offset(TEXT_EDGE_DISTANCE);
    }];
    [self.labelTL layoutIfNeeded];
    //左線
    UIView *leftVLine = [[UIView alloc]init];
    [self.footView addSubview:leftVLine];
    leftVLine.backgroundColor = RGB(141, 139, 140);
    [leftVLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.labelTL.mas_centerY);
        make.right.mas_equalTo(self.labelTL.mas_left).mas_offset(-TEXT_EDGE_DISTANCE);
        make.size.mas_equalTo(CGSizeMake(33, 0.5));
    }];
    //右線
    UIView *rightVLine = [[UIView alloc]init];
    [self.footView addSubview:rightVLine];
    rightVLine.backgroundColor = RGB(141, 139, 140);
    [rightVLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.labelTL.mas_centerY);
        make.left.mas_equalTo(self.labelTL.mas_right).mas_offset(TEXT_EDGE_DISTANCE);
        make.size.mas_equalTo(CGSizeMake(33, 0.5));
    }];
}

#pragma mark -- collectionVeiw delegate --
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 10;
}
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    SearchGoodsColCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SearchGoodsColCell_order" forIndexPath:indexPath];
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"選擇商品 %ld",indexPath.row);
    
}
#pragma mark -- tableview delegate --

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //可以注釋
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.0001;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -- clickedDingDanXiangQingaBtn 訂單詳情 --
-(void)clickedDingDanXiangQingaBtn:(UIButton *)sender{
    NSLog(@"clickedDingDanXiangQingaBtn 訂單詳情");
    MineOrderDetailViewController *vc = [[MineOrderDetailViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark -- clickedLiJIPIngJiaBtn --
-(void)clickedLiJIPIngJiaBtn:(UIButton *)sender{
    NSLog(@"clickedLiJIPIngJiaBtn 評價");
    MinePublishPingLunViewController *vc = [[MinePublishPingLunViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark -- clickedRightBtn 右鍵 --
-(void)clickedRightBtn:(UIButton *)sender{
    NSLog(@"clickedRightBtn 右鍵");
    NSMutableArray *arr_tit = [@[@"首頁  ",@"幫助  ",@"我要吐槽"] mutableCopy];
    NSMutableArray *arr_tu = [@[@"shouye-biao",@"bangzhu-biao",@"woyaotucao-biao"] mutableCopy];
    [YBPopupMenu showRelyOnView:sender titles:arr_tit icons:arr_tu menuWidth:120 otherSettings:^(YBPopupMenu *popupMenu) {
        popupMenu.delegate = self;
        popupMenu.fontSize = 12;
        popupMenu.textColor = TEXT_HEX_LIGHT_L_GRAY;
    }];
}
#pragma mark - YBPopupMenuDelegate
- (void)ybPopupMenuDidSelectedAtIndex:(NSInteger)index ybPopupMenu:(YBPopupMenu *)ybPopupMenu
{
    NSLog(@"點選了 %ld 選項",index);
}
#pragma mark -- goBackPage 傳回 --
-(void)goBackPage:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
           

繼續閱讀