ios 怎么获取uicollectionview的可见单元格

ios 怎么获取uicollectionview的可见单元格,第1张

UICollectionView 和 UICollectionViewController 类是iOS6 新引进的API,用于展示集合视图,布局更加灵活,可实现多列布局,用法类似于UITableView 和 UITableViewController 类。

使用UICollectionView 必须实现UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout这三个协议。

下面先给出常用到的一些方法。(只给出常用的,其他的可以查看相关API)

#pragma mark -- UICollectionViewDataSource

//定义展示的UICollectionViewCell的个数

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

{

return 30;

}

//定义展示的Section的个数

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView )collectionView

{

return 1;

}

//每个UICollectionView展示的内容

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

{

static NSString CellIdentifier = @"GradientCell";

UICollectionViewCell cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

cellbackgroundColor = [UIColor colorWithRed:((10 indexPathrow) / 2550) green:((20 indexPathrow)/2550) blue:((30 indexPathrow)/2550) alpha:10f];

return cell;

}

#pragma mark --UICollectionViewDelegateFlowLayout

//定义每个UICollectionView 的大小

- (CGSize)collectionView:(UICollectionView )collectionView layout:(UICollectionViewLayout)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath )indexPath

{

return CGSizeMake(96, 100);

}

//定义每个UICollectionView 的 margin

-(UIEdgeInsets)collectionView:(UICollectionView )collectionView layout:(UICollectionViewLayout )collectionViewLayout insetForSectionAtIndex:(NSInteger)section

{

return UIEdgeInsetsMake(5, 5, 5, 5);

}

#pragma mark --UICollectionViewDelegate

//UICollectionView被选中时调用的方法

-(void)collectionView:(UICollectionView )collectionView didSelectItemAtIndexPath:(NSIndexPath )indexPath

{

UICollectionViewCell cell = (UICollectionViewCell )[collectionView cellForItemAtIndexPath:indexPath];

cellbackgroundColor = [UIColor whiteColor];

}

//返回这个UICollectionView是否可以被选择

-(BOOL)collectionView:(UICollectionView )collectionView shouldSelectItemAtIndexPath:(NSIndexPath )indexPath

{

return YES;

}

可以使用自定义的cell,然后往里面放label,为label赋tag值,通过tag值去获取label,然后获取label中的内容。

例:UILabel textLabel = [cell viewWithTag:10];

NSString text = textLabeltext;

在IOS开发中,很多时候项目需求会有点击某一行,展开cell下面的cell,在此点击会收起;

这种实现方式很多,个人这里随便推荐一种实现方式:

1、在数据转模型的model里面添加一个属性, @property ( assign , nonatomic ) BOOL isOpen; //是否展开

2、在对应的tableView层,- ( nullable UIView)tableView:(UITableView)tableView viewForHeaderInSection:(NSInteger)section;方法下面生成View,并未其添加点击事件;点击事件方法如下:

在对应的tableView的dataSource方法中:

以及- (UITableViewCell)tableView:(UITableView)tableView cellForRowAtIndexPath:(NSIndexPath)indexPath{  if ( self dataList[indexPathsection]isOpen) {

  if ( self dataList[indexPathsection]isOpen) {

//设置展开的cell

}else{

//设置没有展开的cell;

}}

实现的方法和tableBView差不多,实现他的几个代理方法:

解释一下:传进来的item:代表他给你的model,根据这个model可以去获取上级model: clickpackagesModelpackageModel = [treeViewparentForItem:model];

同样可以根据model获取相应的cell: clickPackageCellpackagecell01 = (clickPackageCell)[treeViewcellForItem:model];

有了上面两个方法,基本就能玩转三级展开和收起;

对于三级展开,大家还有什么好的想法亦或者好的框架没?欢迎补充

一般的话- (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath )indexPath 这个方法里应该都会设置cell内的显示内容,在didselect里面你把上面那个方法里的[xxxx objectAtIndex:indexPathrow] xxxxx]再次赋给你自己要取指的对象不就可以了么

以上就是关于ios 怎么获取uicollectionview的可见单元格全部的内容,包括:ios 怎么获取uicollectionview的可见单元格、ios 得到点击cell里的内容、IOS开发,tableView点击cell,具备三级或者更多层级的开发等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/9656825.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-30
下一篇 2023-04-30

发表评论

登录后才能评论

评论列表(0条)

保存