我想知道如何设置我的UICollectionView每个部分最多可以选择1个单元格.我看到有allowsMultipleSelectionUICollectionView 的属性,但我想知道如何防止在同一部分中选择多个单元格.
我是否需要在– collectionView:shouldSelectItemAtIndexPath:和collectionView:shouldDeselectItemAtIndexPath:方法中实现逻辑或者是否有更简单的方法?
谢谢!
我有UICollectionView一个习惯UICollectionViewCell.我alpha在UICollectionViewCellto中设置了0.0f一个特殊任务的属性,它都按预期工作.但是,当我将视图旋转到横向时,单元格将alpha自身重置为1.0.
我玩过这个并且集合视图保存所选单元格和删除单元格等的状态,但不包含单元格的alpha值.
我试图重新设置已更改单元格的alpha值,didRotateFromInterfaceOrientation:但它会1.0在调用完成后将其更改回原来的状态.
我试图弄清楚UICollectionViewCell旋转后α值的重置位置.
如果我运行下面的代码,我会为单元格返回所需的alpha值,但是在此之后的某个时刻,它们都会变回1.0:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
//array containing IndexPaths for cells with 0.0f alpha value
for (NSIndexPath *path in self.selectedlettersArray) {
LetterCellView* lsv = (LetterCellView*) [self.collectionView cellForItemAtIndexPath:path];
lsv.alpha = 0.0f;
}
for (LetterCellView *lsv in self.collectionView.visibleCells) {
NSLog(@"After Cell Alpha is : %f", lsv.alpha);
}
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
嗨我创建了一个UIViewController,我添加了一个UICollectionViewCustomCell.然后,我uicollectionviewcell为细胞创建了一个类.最后在storybord本身设置委托和数据源.但它显示空页.如下图所示

码:
- (void)viewDidLoad
{
[super viewDidLoad];
self.collectionview = _collectionview;
self.collectionview.dataSource = self;
self.collectionview.delegate = self;
speakersImages=[[NSMutableArray alloc]init];
// Do any additional setup after loading the view, typically from a nib.
imageLabels=[[NSMutableArray alloc]initWithObjects:@"RB Forum 2013",@"Agenda",@"Speakers",@"Contact",@"Map",@"Websites",@"@Responsible Biz",@"Partners",@"Sustainability",nil];
imagePaths=[[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"RBform.png"],[UIImage imageNamed:@"agenda.png"],[UIImage imageNamed:@"speakers.png"],[UIImage imageNamed:@"contact.png"],[UIImage imageNamed:@"map.png"],[UIImage imageNamed:@"website.png"],[UIImage imageNamed:@"twitter.png"],[UIImage imageNamed:@"partners.png"],[UIImage imageNamed:@"sustainability.png"], nil];
[self.collectionview registerClass:[NewCell class] forCellWithReuseIdentifier:@"Cell"];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section
{
return [imageLabels count];
}
//THE BELOW DELEGATE METHOD DOES …Run Code Online (Sandbox Code Playgroud) storyboard nsarray ios uicollectionview uicollectionviewcell
我想把我从flickr获得的图像加载到uicollectionsview我跟随分配指南但是当我必须这样做时它们都会分崩离析
cell.imageView.image
Run Code Online (Sandbox Code Playgroud)
它基本上说它无法在uicollectionsviewcell的对象中找到imageView
我也试过这样的方法,但它没有用
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"flickerCell";
UICollectionViewCell *flickerCell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
flickerCell.backgroundColor = [UIColor whiteColor];
UIImageView *recipeImageView = (UIImageView *)[flickerCell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:[photoURLs objectAtIndex:indexPath.row]];
[flickerCell addSubview:recipeImageView];
return flickerCell;
}
Run Code Online (Sandbox Code Playgroud)
recipieImageView是我得到一些教程的东西.我的单元格在故事板中被命名为flickerCell,而且photURLs有33个我可以在代码中查看的对象.所以它就在那里.还有photoURLs是一个NSMutablearray.在这个方法中我的recipeImageView返回nill ??? 我有一个cell.h,它有imageView
#import <UIKit/UIKit.h>
@interface Cell : UICollectionViewCell
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@end
Run Code Online (Sandbox Code Playgroud)
所以,如果有人知道一种方式,我可以在uicollectionview中显示photoURLs所有的图像,这将是非常有帮助的
更新 我现在尝试了一种新方法,但仍然无法正常工作.imageView返回nil
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"flickerCell";
Cell *flickerCell = (Cell*) [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
flickerCell.backgroundColor = …Run Code Online (Sandbox Code Playgroud) objective-c uiimage ios uicollectionview uicollectionviewcell
我现在设置了水平滚动我希望它能够一次滚动/捕捉一个单元格,同时滚动到一边.
有人能解开一些光吗?这是我到目前为止的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
}
#pragma mark - UICollectionView Datasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 1;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 20;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MyCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
return cell;
}
#pragma mark - UICollectionViewDelegateFlowLayout
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(50, 20, 50, 20);
}
Run Code Online (Sandbox Code Playgroud) 我有一个集合视图,我想让每行说4个单元格.我知道要完成这一切,我需要做的就是除以collectionview.frame.size.width4.这很容易.但是,我无法弄清楚的是,如何考虑集合视图侧面的插图和单元格之间的间距.我在集合视图的左侧和右侧有10个像素插图,并且单元格之间有10个像素的间距.如何考虑这10个px插图来计算所需的单元格宽度?
我正在开发一款iPad应用程序,它应该能够显示4个彼此相邻的CollectionView.集合视图的高度应该是屏幕的3/4,因此布局看起来像这样:
___________________
| top content |
|-------------------|
| CV | CV | CV | CV |
|____|____|____|____|
Run Code Online (Sandbox Code Playgroud)
我试图缩小所有内容并创建一个只有其中一个集合视图的新项目,但我仍然遇到同样的问题:当我点击其中一个单元格时,它们都会消失.重现:
CollectionViewCell.swift :(通过ctrl拖动标签创建出口到源代码)
import UIKit
class CollectionViewCell: UICollectionViewCell {
@IBOutlet weak var label: UILabel!
}
Run Code Online (Sandbox Code Playgroud)
CollectionViewController.swift :(注意viewDidLoad实现中的注释)
import UIKit
private let reuseIdentifier = "MyCollectionViewCell"
class CollectionViewController: UICollectionViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Register cell classes
// this has to be removed to work with a custom cell class
// self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: …Run Code Online (Sandbox Code Playgroud) 当我打印我在MyCell类中声明的IBOutlets时,它们就是零.
ViewController中的方法
public override func viewDidLoad() {
super.viewDidLoad()
self.seriesCollectionView.register(MyCell.self, forCellWithReuseIdentifier: "MyCell")
}
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath) as! MyCell
cell.setup()
return cell
}
Run Code Online (Sandbox Code Playgroud)
MyCell类
class MTSeriesViewCell: UICollectionViewCell {
@IBOutlet weak var cellView: UIView!
@IBOutlet weak var cellImageView: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
}
public func setup() {
print(cellView)
}
}
Run Code Online (Sandbox Code Playgroud)
我是否应该在ViewController的viewDidLoad中初始化/注册MyCell类?
我正在使用xcode 8.3。我有一个CollectionView。我已经从Web服务下载了图像并将图像放置在每个CollectionView单元中。但是,当我滚动CollectionView时,每个单元格中的图像都在变化。几分钟后,它将显示正确的图像。我已经尝试了许多stackoverflow中可用的解决方案。但我没有得到解决方案。请帮我。
objective-c ios uicollectionview uicollectionviewcell uicollectionviewlayout
在我的应用程序中,我有一个全屏的分页集合视图,每个单元也需要全屏显示,因此集合视图布局的项目大小必须与视图控制器的视图边界大小相同。为此,viewDidLayoutSubviews我只是设置项目大小,并且它按预期工作。当我显示此屏幕时,它viewDidLayoutSubviews被调用3次,并且每次iPhone 7的视图边界尺寸为375.0 x 667.0。该单元的尺寸与预期的相同。
但是,当使用3D Touch窥视和弹出显示此屏幕时,无论是窥视还是弹出,单元格的大小都没有达到预期的大小。viewDidLayoutSubviews被称为4次,具有以下边界大小:
--- peek triggered ---
375.0 x 569.524495677234
375.0 x 569.524495677234
375.0 x 720.821325648415
--- pop triggered ---
375.0 x 667.0
Run Code Online (Sandbox Code Playgroud)
窥视时,集合视图将按预期以全屏方式显示,高度为721,单元格的预期宽度为375,但单元格的高度应为569(当单元格高度为721时)。在弹出时,集合视图的高度将更改达到预期的667,但像元高度仍为569。
我尝试collectionView.layoutIfNeeded()在设置后致电,itemSize但结果是一样的。为什么集合视图不更新此处的单元格大小?
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
print("\(view.bounds.width) x \(view.bounds.height)")
let boundsSize = CGSize(width: Int(view.bounds.width), height: Int(view.bounds.height))
let flowLayout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
if flowLayout.itemSize != boundsSize {
flowLayout.itemSize = boundsSize
collectionView.layoutIfNeeded() //this doens't help
}
}
Run Code Online (Sandbox Code Playgroud) ios uicollectionview uicollectionviewcell uicollectionviewlayout