iOS UICollectionView實(shí)現(xiàn)橫向滑動(dòng)
本文實(shí)例為大家分享了iOS UICollectionView實(shí)現(xiàn)橫向滑動(dòng)的具體代碼,供大家參考,具體內(nèi)容如下
UICollectionView的橫向滾動(dòng),目前我使用在了顯示輸入框的輸入歷史上;
//
// SCVisitorInputAccessoryView.m
// 訪客通行錄入頁面--訪客姓名輸入歷史的InputAccessory
#import "SCInputAccessoryView.h"
#import "SCInputAccessoryCell.h"
#define SCHorizontalMargin 15.0f
#define SCVerticalMargin 10.0f
@interface SCInputAccessoryView () <UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
/// 名字記錄的數(shù)組
@property (nonatomic, strong) NSMutableArray *nameArray;
@end
@implementation SCInputAccessoryView
+ (instancetype)loadNibView {
return [[[NSBundle mainBundle] loadNibNamed:[SCInputAccessoryView className] owner:self options:nil] objectAtIndex:0];
}
- (void)awakeFromNib {
[super awakeFromNib];
self.clipsToBounds = YES;
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self setupView];
}
- (void)setupView {
/// 設(shè)置此屬性為yes 不滿一屏幕 也能滾動(dòng)
self.collectionView.alwaysBounceHorizontal = YES;
self.collectionView.showsHorizontalScrollIndicator = NO;
// 1.創(chuàng)建流水布局
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self.collectionView.collectionViewLayout = layout;
[self registerNibWithTableView];
}
#pragma mark - 代理方法 Delegate Methods
// 設(shè)置分區(qū)
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
// 每個(gè)分區(qū)上得元素個(gè)數(shù)
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.nameArray.count;
}
// 設(shè)置cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
SCInputAccessoryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([SCInputAccessoryCell class]) forIndexPath:indexPath];
[cell refreshCellWithTitle:self.nameArray[indexPath.row]];
return cell;
}
// 設(shè)置cell大小 itemSize:可以給每一個(gè)cell指定不同的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGFloat height = 35.0f;
CGFloat width = [self gainStringWidthWithString:self.nameArray[indexPath.row] font:15.0f height:height];
return CGSizeMake(width, height);
}
// 設(shè)置UIcollectionView整體的內(nèi)邊距(這樣item不貼邊顯示)
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
// 上 左 下 右
return UIEdgeInsetsMake(SCVerticalMargin, SCHorizontalMargin, SCVerticalMargin, SCHorizontalMargin);
}
// 設(shè)置minimumLineSpacing:cell上下之間最小的距離
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return SCHorizontalMargin;
}
// 設(shè)置minimumInteritemSpacing:cell左右之間最小的距離
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return SCHorizontalMargin;
}
// 選中cell的回調(diào)
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if (self.selectRowBlock) {
self.selectRowBlock(indexPath.row, self.nameArray[indexPath.row]);
}
}
#pragma mark - 對(duì)外方法 Public Methods
/// array數(shù)組里面放的元素 必須字符串類型的
- (void)refreshUIWithNameArray:(NSArray<NSString *> *)array {
[self.nameArray removeAllObjects];
[self.nameArray addObjectsFromArray:array];
[self.collectionView reloadData];
}
#pragma mark - 內(nèi)部方法 Private Methods
// 注冊(cè)cell
- (void)registerNibWithTableView {
[self.collectionView registerNib:[UINib nibWithNibName:[SCInputAccessoryCell className] bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([SCInputAccessoryCell class])];
}
- (CGFloat)gainStringWidthWithString:(NSString *)string font:(CGFloat)font height:(CGFloat)height {
if (string.length == 0) {
return 0.0f;
}
CGSize maxSize = CGSizeMake(MAXFLOAT, height);
CGSize realSize = [string boundingRectWithSize:maxSize
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]}
context:nil].size;
/// 左右各16
return (realSize.width + 2 * (SCHorizontalMargin + 1.f));
}
#pragma mark - 點(diǎn)擊/觸碰事件 Action Methods
#pragma mark - 懶加載 Lazy Load
- (NSMutableArray *)nameArray {
if (!_nameArray) {
_nameArray = [NSMutableArray arrayWithCapacity:0];
}
return _nameArray;
}
@end
效果圖:

Demo地址 :XGDevelopDemo
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
講解iOS開發(fā)中基本的定位功能實(shí)現(xiàn)
這篇文章主要介紹了講解iOS開發(fā)中基本的定位功能實(shí)現(xiàn),示例基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-10-10
Objective-C實(shí)現(xiàn)冒泡排序算法的簡單示例
冒泡排序即是依次比較相鄰的兩個(gè)數(shù),如果后面的數(shù)較小則交換到前面一個(gè)數(shù)的位置上,這里我們來看一下Objective-C實(shí)現(xiàn)冒泡排序算法的簡單示例2016-06-06
關(guān)于ios配置微信config出現(xiàn)驗(yàn)簽失敗的問題解決
這篇文章主要介紹了關(guān)于ios配置微信config出現(xiàn)驗(yàn)簽失敗的問題解決方案,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
iOS開發(fā)實(shí)現(xiàn)HTTPS之cer文件的使用詳解
下面小編就為大家分享一篇iOS開發(fā)實(shí)現(xiàn)HTTPS之cer文件的使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01
IOS 上架后出現(xiàn)90034代碼錯(cuò)誤問題解決
這篇文章主要介紹了IOS 上架后出現(xiàn)90034代碼錯(cuò)誤問題解決的相關(guān)資料,需要的朋友可以參考下2016-11-11

