网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

怎么在iOS中利用UICollectionView实现横向滑动-创新互联

这期内容当中小编将会给大家带来有关怎么在iOS中利用UICollectionView实现横向滑动,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

10年积累的成都网站制作、成都网站建设、外贸营销网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站制作后付款的网站建设流程,更有云州免费网站建设让你可以放心的选择与我们合作。

UICollectionView的横向滚动

//
// SCVisitorInputAccessoryView.m
// 访客通行录入页面--访客姓名输入历史的InputAccessory

#import "SCInputAccessoryView.h"
#import "SCInputAccessoryCell.h"

#define SCHorizontalMargin 15.0f
#define SCVerticalMargin 10.0f

@interface SCInputAccessoryView () 

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

/// 名字记录的数组
@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 {

 /// 设置此属性为yes 不满一屏幕 也能滚动
 self.collectionView.alwaysBounceHorizontal = YES;
 self.collectionView.showsHorizontalScrollIndicator = NO;
 // 1.创建流水布局
 UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
 layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
 self.collectionView.collectionViewLayout = layout;
 [self registerNibWithTableView];
}

#pragma mark - 代理方法 Delegate Methods
// 设置分区

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
 return 1;
}

// 每个分区上得元素个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
 return self.nameArray.count;
}

// 设置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;
}

// 设置cell大小 itemSize:可以给每一个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);
}


// 设置UIcollectionView整体的内边距(这样item不贴边显示)
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
 // 上 左 下 右
 return UIEdgeInsetsMake(SCVerticalMargin, SCHorizontalMargin, SCVerticalMargin, SCHorizontalMargin);
}

// 设置minimumLineSpacing:cell上下之间最小的距离
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
 return SCHorizontalMargin;
}

// 设置minimumInteritemSpacing:cell左右之间最小的距离
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
 return SCHorizontalMargin;
}

// 选中cell的回调
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
 if (self.selectRowBlock) {
 self.selectRowBlock(indexPath.row, self.nameArray[indexPath.row]);
 }
}

#pragma mark - 对外方法 Public Methods
/// array数组里面放的元素 必须字符串类型的
- (void)refreshUIWithNameArray:(NSArray *)array {
 [self.nameArray removeAllObjects];
 [self.nameArray addObjectsFromArray:array];
 [self.collectionView reloadData];
}


#pragma mark - 内部方法 Private Methods
// 注册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 - 点击/触碰事件 Action Methods

#pragma mark - 懒加载 Lazy Load

- (NSMutableArray *)nameArray {
 if (!_nameArray) {
 _nameArray = [NSMutableArray arrayWithCapacity:0];
 }
 return _nameArray;
}

@end

上述就是小编为大家分享的怎么在iOS中利用UICollectionView实现横向滑动了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联网站建设公司行业资讯频道。

另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前文章:怎么在iOS中利用UICollectionView实现横向滑动-创新互联
文章来源:http://bjjierui.cn/article/dgsdsj.html

其他资讯