您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 列表視圖與網格視圖一鍵切換的簡潔實作

編輯:iOS 開發綜合

先看京東商城的實機截圖

看到這種效果,多數人第一反應是用 TableView 搭配 CollectionView 來切換。筆者最初也這麼想,但後來發現其實只靠一個 CollectionView 就能輕鬆搞定,代碼量還更少。

實作步驟

一、建立 CollectionView

 - (UICollectionView *)collectionView { if (!_collectionView) { UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc] init]; //設置滾動方向 [flowlayout setScrollDirection:UICollectionViewScrollDirectionVertical]; //左右間距 flowlayout.minimumInteritemSpacing = 2; //上下間距 flowlayout.minimumLineSpacing = 2; _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(2 , 2 , self.view.bounds.size.width - 4, self.view.bounds.size.height - 4) collectionViewLayout:flowlayout]; _collectionView.delegate = self; _collectionView.dataSource = self; _collectionView.showsVerticalScrollIndicator = NO; _collectionView.showsHorizontalScrollIndicator = NO; [_collectionView setBackgroundColor:[UIColor clearColor]]; //注冊cell [_collectionView registerClass:[GridListCollectionViewCell class] forCellWithReuseIdentifier:kCellIdentifier_CollectionViewCell]; } return _collectionView; }

接著抓取京東商城的 json 資料並解析成模型,objectWithDictionary: 負責把字典轉為模型,這是筆者用 Runtime 封裝的工具,一行就能完成解析。

 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // 默認列表視圖 _isGrid = NO; NSString *path = [[NSBundle mainBundle] pathForResource:@"product" ofType:@"json"]; NSData *data = [NSData dataWithContentsOfFile:path]; NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; [self.view addSubview:self.collectionView]; NSArray *products = dict[@"wareInfo"]; for (id obj in products) { [self.dataSource addObject:[GridListModel objectWithDictionary:obj]]; } }

然後自訂 CollectionViewCell,為 cell 加上一個 isGrid 屬性,用來判斷當前是列表還是網格樣式。

.h 檔:

 #import <UIKit/UIKit.h> #define kCellIdentifier_CollectionViewCell @"GridListCollectionViewCell" @class GridListModel; @interface GridListCollectionViewCell : UICollectionViewCell /** 0:列表視圖,1:格子視圖 */ @property (nonatomic, assign) BOOL isGrid; @property (nonatomic, strong) GridListModel *model; @end

.m 檔

 #import "GridListCollectionViewCell.h" #import "GridListModel.h" #import "UIImageView+WebCache.h" #define ScreenWidth ([UIScreen mainScreen].bounds.size.width) @interface GridListCollectionViewCell () @property (nonatomic, strong) UIImageView *imageV; @property (nonatomic, strong) UILabel *titleLabel; @property (nonatomic, strong) UILabel *priceLabel; @end @implementation GridListCollectionViewCell - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self configureUI]; } return self; } - (void)configureUI { _imageV = [[UIImageView alloc] initWithFrame:CGRectZero]; [self.contentView addSubview:_imageV]; _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; _titleLabel.numberOfLines = 0; _titleLabel.font = [UIFont boldSystemFontOfSize:14]; [self.contentView addSubview:_titleLabel]; _priceLabel = [[UILabel alloc] initWithFrame:CGRectZero]; _priceLabel.textColor = [UIColor redColor]; _priceLabel.font = [UIFont systemFontOfSize:16]; [self.contentView addSubview:_priceLabel]; } - (void)setIsGrid:(BOOL)isGrid { _isGrid = isGrid; if (isGrid) { _imageV.frame = CGRectMake(5, 5, self.bounds.size.width - 60, self.bounds.size.width - 60); _titleLabel.frame = CGRectMake(5, self.bounds.size.width - 45, ScreenWidth/2, 20); _priceLabel.frame = CGRectMake(5, self.bounds.size.width - 20, ScreenWidth/2, 20); } else { _imageV.frame = CGRectMake(5, 5, self.bounds.size.height - 10, self.bounds.size.height - 10); _titleLabel.frame = CGRectMake(self.bounds.size.height + 10, 0, ScreenWidth/2, self.bounds.size.height - 20);; _priceLabel.frame = CGRectMake(self.bounds.size.height + 10, self.bounds.size.height - 30, ScreenWidth/2, 20);; } } - (void)setModel:(GridListModel *)model { _model = model; [_imageV sd_setImageWithURL:[NSURL URLWithString:model.imageurl]]; _titleLabel.text = model.wname; _priceLabel.text = [NSString stringWithFormat:@"¥%.2f",model.jdPrice]; } @end

再加一個切換視圖的按鈕,點擊事件如下:

 #pragma mark - Action - (IBAction)onBtnClick:(id)sender { _isGrid = !_isGrid; [self.collectionView reloadData]; if (_isGrid) { [self.swithBtn setImage:[UIImage imageNamed:@"product_list_grid_btn"] forState:0]; } else { [self.swithBtn setImage:[UIImage imageNamed:@"product_list_list_btn"] forState:0]; } }

最後,別忘了在切換時調整 CollectionView 的 ItemSize。

 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { if (_isGrid) { return CGSizeMake((ScreenWidth - 6) / 2, (ScreenWidth - 6) / 2 + 40); } else { return CGSizeMake(ScreenWidth - 4, (ScreenWidth - 6) / 4 + 20); } }

如此一來,列表視圖與網格視圖的雙向切換就實現了,是不是相當簡單?

小結

以上就是本文的全部內容。由於筆者能力有限,若有疏漏之處還望不吝指正;如果你有更棒的做法或建議,也歡迎一起交流探討,希望這篇文章能給你帶來幫助。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved