ios實現下拉重新整理,上拉載入

來源:互聯網
上載者:User

標籤:tla   row   nav   round   path   btn   free   下拉重新整理   名稱   

1、自訂cell

//#import "EnjoyListModel.h"

 

@protocol EnjoyCollectionCellOfViewDelegate <NSObject>

 

- (void)freeButtonDelegateAction:(UIButton *)sender;

 

@end

 @interface EnjoyCollectionCellOfView : UICollectionViewCell

 @property (nonatomic,strong)id<EnjoyCollectionCellOfViewDelegate>delegate;

/*免費拿按鈕*/

@property (nonatomic,strong)UIButton *freeButton; 

//- (void)updateEnjoyCollectionCellOfViewModel:(EnjoyListModel *)model;

@end

.m檔案實現

#import "EnjoyCollectionCellOfView.h"

#import "Masonry.h"

//#import "UIImageView+WebCache.h"

#define EnjoyCollectionViewLeftMargin 0

#define EnjoyCollectionViewTopMargin  0

 

 

@interface EnjoyCollectionCellOfView ()

 

/*商品圖片*/

@property (nonatomic,strong)UIImageView *goodsImageView;

/*商品名稱*/

@property (nonatomic,strong)UILabel *goodsTitleLabel;

/*收益和名稱*/

@property (nonatomic,strong)UILabel *goodsLastLabel;

 

 

@end

 

@implementation EnjoyCollectionCellOfView

 

- (instancetype)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self)

    {

        self.backgroundColor = [UIColor whiteColor];

     __weak __typeof(&*self)weakSelf = self;

        

        [self.contentView addSubview:self.goodsImageView];

        [self.goodsImageView mas_makeConstraints:^(MASConstraintMaker *make)

         {

             make.left.mas_equalTo(weakSelf.mas_left).offset(EnjoyCollectionViewLeftMargin);

             make.top.mas_equalTo(weakSelf.mas_top).offset(EnjoyCollectionViewTopMargin);

             make.width.mas_equalTo(weakSelf.contentView.mas_width).offset(0);

             make.height.mas_equalTo(weakSelf.contentView.mas_width).offset(0);

        }];

        

        [self.contentView addSubview:self.goodsTitleLabel];

        [self.goodsTitleLabel mas_makeConstraints:^(MASConstraintMaker *make)

        {

            make.left.mas_equalTo(weakSelf.mas_left).offset(EnjoyCollectionViewLeftMargin);

            make.right.mas_equalTo(weakSelf.mas_right).offset(-EnjoyCollectionViewLeftMargin);

            make.top.mas_equalTo(weakSelf.goodsImageView.mas_bottom).offset(0);

            make.height.mas_offset(20);

            

        }];

        

        [self.contentView addSubview:self.goodsLastLabel];

        [self.goodsLastLabel mas_makeConstraints:^(MASConstraintMaker *make)

        {

            make.left.mas_equalTo(weakSelf.mas_left).offset(EnjoyCollectionViewLeftMargin);

            make.right.mas_equalTo(weakSelf.mas_right).offset(-EnjoyCollectionViewLeftMargin);

            make.top.mas_equalTo(weakSelf.goodsTitleLabel.mas_bottom).offset(0);

            make.height.mas_offset(20);

        }];

        

        [self.contentView addSubview:self.freeButton];

        [self.freeButton mas_makeConstraints:^(MASConstraintMaker *make)

         {

        

             make.left.mas_equalTo(weakSelf.mas_left).offset(35);

             make.right.mas_equalTo(weakSelf.mas_right).offset(-35);

             make.bottom.mas_equalTo(weakSelf.contentView.mas_bottom).offset(-10);

             make.height.mas_offset(30);

        }];

        

        [self.freeButton addTarget:self action:@selector(freeButtonAction:) forControlEvents:UIControlEventTouchUpInside];

        

    }

    return self;

}

 

#pragma mark - buttonAction

- (void)freeButtonAction:(UIButton *)sender

{

    if ([self.delegate respondsToSelector:@selector(freeButtonDelegateAction:)])

    {

        

        [self.delegate freeButtonDelegateAction:sender];

    }

    

    

}

 

#pragma mark - updateUI

 

 

 

#pragma mark -- lazy

- (UIImageView *)goodsImageView

{

    if (!_goodsImageView)

    {

        _goodsImageView = [[UIImageView alloc]init];

        _goodsImageView.backgroundColor = [UIColor redColor];

        

    }

    return _goodsImageView;

}

 

 

- (UILabel *)goodsTitleLabel

{

    if (!_goodsTitleLabel) {

        _goodsTitleLabel = [[UILabel alloc]init];

        _goodsTitleLabel.text = @"商品名稱";

//        _goodsTitleLabel.backgroundColor = [UIColor lightGrayColor];

        _goodsTitleLabel.textAlignment = NSTextAlignmentCenter;

        _goodsTitleLabel.font = [UIFont systemFontOfSize:12];

    }

    return _goodsTitleLabel;

}

 

- (UILabel *)goodsLastLabel

{

    if (!_goodsLastLabel) {

        _goodsLastLabel = [[UILabel alloc]init];

        _goodsLastLabel.text = @"商品名稱";

//        _goodsLastLabel.backgroundColor = [UIColor yellowColor];

        _goodsLastLabel.textAlignment = NSTextAlignmentCenter;

        _goodsLastLabel.font = [UIFont systemFontOfSize:12];

    }

    return _goodsLastLabel;

}

 

 

- (UIButton *)freeButton

{

    if (!_freeButton)

    {

        _freeButton = [UIButton buttonWithType:UIButtonTypeCustom];

        [_freeButton setTitle:@"隨手拿" forState:UIControlStateNormal];

//        [_freeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

        [_freeButton setBackgroundColor:[UIColor redColor]];

        _freeButton.layer.masksToBounds = YES;

        _freeButton.layer.cornerRadius = 5;

        

    }

    return _freeButton;

}

@end

其中

EnjoyWaterFlowLayout與我上篇是一樣的,可以前去觀看

然後我的collectionView是放在一個view中的,不是放在VC中的

#import <UIKit/UIKit.h>

@protocol ZoneOfEnjoyViewDelegate  <NSObject>

 

//- (void)freeBtnGotoBuyView:(UIButton *)sender withListModel:(EnjoyListModel *)listModel;

 

@end

 

@interface ZoneOfEnjoyView : UIView

 

@property (nonatomic,assign)id<ZoneOfEnjoyViewDelegate>delegate;

@property (nonatomic,strong)UICollectionView *collectionView;

 

/*注釋*/

@property (nonatomic,strong)NSMutableArray *goodsMuArray;

@end

.m檔案實現

#define kMainScreenWidth    [[UIScreen mainScreen] applicationFrame].size.width

#define kMainScreenHeight   [[UIScreen mainScreen] applicationFrame].size.height

 

#define kDeviceWidth        [UIScreen mainScreen].bounds.size.width      // 介面寬度

#define kDeviceHeight       [UIScreen mainScreen].bounds.size.height     // 介面高度

 

#define kStatusBarHeight    [[UIApplication sharedApplication] statusBarFrame].size.height

#define kHeight             (kDeviceHeight - kStatusBarHeight - kNavigationBarHeight)   // 高度

#define kTabBarHeight       49.0

 

#import "ZoneOfEnjoyView.h"

//#import "EnjoyBannerView.h"

#import "EnjoyCollectionCellOfView.h"

#import "EnjoyWaterFlowLayout.h"

 

@interface ZoneOfEnjoyView ()<UICollectionViewDelegate,UICollectionViewDataSource,WaterFlowLayoutDelegate,EnjoyCollectionCellOfViewDelegate>

{

    

}

 

//@property (nonatomic,strong)EnjoyBannerView *bannerView;

 

/*注釋*/

//@property (nonatomic,strong)NSMutableArray *goodsMuArray;

 

 

 

 

@end

 

static NSString *const CellIdentifier = @"EnjoyCollectionCellOfViewCell";

@implementation ZoneOfEnjoyView

 

- (instancetype)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self)

    {

        EnjoyWaterFlowLayout *layout = [[EnjoyWaterFlowLayout alloc]init];

        layout.delegate = self;

         self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kDeviceWidth, kDeviceHeight  - 10 - kTabBarHeight - 44 ) collectionViewLayout:layout];

        self.collectionView.backgroundColor = [UIColor whiteColor];

        _collectionView.dataSource = self;

        _collectionView.delegate = self;

       

        [self addSubview:self.collectionView];

 

        [self.collectionView registerClass:[EnjoyCollectionCellOfView class] forCellWithReuseIdentifier:CellIdentifier];

        

//        if (!self.goodsMuArray)

//        {

//            self.goodsMuArray = [NSMutableArray array];

//        }

        

    }

    return self;

}

 

 

- (void)setGoodsMuArray:(NSMutableArray *)goodsMuArray

{

    _goodsMuArray = goodsMuArray;

    

    if (_goodsMuArray.count)

    {

        [self.goodsMuArray addObjectsFromArray:goodsMuArray];

        [self.collectionView reloadData];

    }

    else

    {

        return;

    }

    

}

 

 

#pragma mark -- UICollectionViewDataSource

 

#pragma mark - collection資料來源代理

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

{

    if (self.goodsMuArray.count)

    {

        return self.goodsMuArray.count;

    }

    else

    {

        return 0;

    }

}

 

 

- ( UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{

//    EnjoyListModel *model = [[EnjoyListModel alloc]init];

    

    

    EnjoyCollectionCellOfView *cell = (EnjoyCollectionCellOfView *)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.freeButton.tag = indexPath.row;

    cell.delegate = self;

    

    if (self.goodsMuArray.count)

    {

//        model = self.goodsMuArray[indexPath.row];

//        [cell updateEnjoyCollectionCellOfViewModel:model];

    }

    else

    {

    }

    

    return cell;

}

 

 

 

- (CGFloat)WaterFlowLayout:(EnjoyWaterFlowLayout *)WaterFlowLayout heightForRowAtIndexPath:(NSInteger )index itemWidth:(CGFloat)itemWidth

{

 

    return (kDeviceWidth)/2 + 75;

 

    

}

 

@end

 

最後在VC中實現

 

#import "ViewController.h"

#import "ZoneOfEnjoyView.h"

#import "MJRefresh.h"

 

 

#define kDeviceWidth        [UIScreen mainScreen].bounds.size.width      // 介面寬度

#define kDeviceHeight       [UIScreen mainScreen].bounds.size.height     // 介面高度

 

@interface ViewController ()

 

/*注釋*/

@property (nonatomic,strong)ZoneOfEnjoyView *enjoyView;

/**

 *  存放假資料

 */

@property (strong, nonatomic) NSMutableArray *fakeColors;

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    self.enjoyView = [[ZoneOfEnjoyView alloc]initWithFrame:CGRectMake(0, 0, kDeviceWidth, kDeviceHeight - 45)];

    [self.view addSubview:self.enjoyView];

    

    if (!self.fakeColors) {

        self.fakeColors = [NSMutableArray array];

    }

    

    self.enjoyView.goodsMuArray = [NSMutableArray array];

    // Do any additional setup after loading the view, typically from a nib.

}

 

- (void)viewWillAppear:(BOOL)animated

{

//    NSArray *array [email protected][@"1111",@"2222",@"1111"];

//    [self.enjoyView resetZoneOfEnjoyView:array withIndex:1];

    

    // 2.整合重新整理控制項

    [self addHeader];

    [self addFooter];

    

}

 

- (void)addHeader

{

    __unsafe_unretained typeof(self) vc = self;

    

    __block typeof(self)WeakSelf = self;

    // 添加下拉重新整理頭部控制項

    

    NSMutableArray *array = [NSMutableArray array];

    [WeakSelf.enjoyView.collectionView addHeaderWithCallback:^{

        // 進入重新整理狀態就會回調這個Block

        

        // 增加5條假資料

        for (int i = 0; i<5; i++)

        {

            [WeakSelf.enjoyView.goodsMuArray insertObject:@"0000" atIndex:0];

        }

        

        

 

       

        

        // 類比消極式載入資料,因此2秒後才調用)

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

            [vc.enjoyView.collectionView reloadData];

            // 結束重新整理

            [vc.enjoyView.collectionView headerEndRefreshing];

        });

    }];

    

#warning 自動重新整理(一進入程式就下拉重新整理)

    [self.enjoyView.collectionView headerBeginRefreshing];

}

 

- (void)addFooter

{

    __unsafe_unretained typeof(self) vc = self;

      __block typeof(self)WeakSelf = self;

    // 添加上拉重新整理尾部控制項

    [WeakSelf.enjoyView.collectionView addFooterWithCallback:^{

        // 進入重新整理狀態就會回調這個Block

        

        // 增加5條假資料

        for (int i = 0; i<5; i++) {

            [WeakSelf.enjoyView.goodsMuArray addObject:@"1111"];

        }

//       [WeakSelf.enjoyView resetZoneOfEnjoyView:[NSArray arrayWithArray:vc.fakeColors] withIndex:0];

        

        // 類比消極式載入資料,因此2秒後才調用)

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

            [vc.enjoyView.collectionView reloadData];

            // 結束重新整理

            [vc.enjoyView.collectionView footerEndRefreshing];

        });

    }];

}

 

/**

 為了保證內部不泄露,在dealloc中釋放佔用的記憶體

 */

- (void)dealloc

{

    NSLog(@"MJCollectionViewController--dealloc---");

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

ios實現下拉重新整理,上拉載入

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.