我有一个数组,其中包含先前选定的单元格的位置。我想通过突出显示并选择它们(如果我单击之前选择的一个单元格,我希望它取消选择它),向用户显示之前选择的单元格(在另一个时刻,可能是几个月前)出现弹出窗口,就像我在中所做的那样:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
Run Code Online (Sandbox Code Playgroud)
我尝试了不同的事情,例如:
if (indexPath.row == 0 && indexPath.section == 0){
collectionView.selectItem(at: indexPath, animated: false, scrollPosition: UICollectionViewScrollPosition.centeredHorizontally)
}
Run Code Online (Sandbox Code Playgroud)
或者 :
let indexPathForFirstRow = IndexPath(item: 0, section: 0)
collectionView.cellForItem(at: indexPathForFirstRow)?.isSelected = true
Run Code Online (Sandbox Code Playgroud)
但没有任何作用(或者我看不到它......)
你有什么帮助来实现这一点吗?谢谢!
编辑:
这是我现在所做的:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// get a reference to our storyboard cell
var cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MyCollectionViewCell
// Use the outlet in our custom class to get …Run Code Online (Sandbox Code Playgroud) 我有两种不同的单元格类型,一种用于评论,另一种用于回复。我试图以相同的方式呈现它们collectionView,然后可能像这样对它们进行分组:每个具有特定 id 的评论下面都有其回复。然而,无论什么尝试,我都失败了。
你会怎样做呢?
private var comments = [Comment]()
private var replies = [Reply]()
var items: [Any] = []
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return items.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// let item = items[indexPath.item]
var item = items[indexPath.item]
if item is Comment.Type {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CommentCell.cellId, for: indexPath) as! CommentCell
cell.comment = items[indexPath.item] as? Comment
print(item)
return cell
} else { …Run Code Online (Sandbox Code Playgroud) 我用这个小小的东西读了所有的问题.FavoritosViewController.m我有:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
favcolCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"fav" forIndexPath:indexPath];
[[cell imageCell]setImage:[UIImage imageNamed:[arrayimages objectAtIndex:indexPath.item]]];
return cell;
}
Run Code Online (Sandbox Code Playgroud)
在favcolCell.h我有:
@property (retain, nonatomic) IBOutlet UIImageView *imageCell;
Run Code Online (Sandbox Code Playgroud)
在favcolCell.m我有:
@synthesize imageCell;
Run Code Online (Sandbox Code Playgroud)
(......)
- (void)dealloc {
[imageCell release];
[super dealloc];
}
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
编辑:
解决方案:做正确的课程注册.
我在IB中定义的ViewController中的UICollectionView中有一个自定义UICollectionViewCell.在单元格中有一个标签,我可以在cellForItemAtIndexPath中设置和修改文本属性.我想要实现的是更改UILabel中所选单元格的文本属性.当在didSelectItemAtIndexPath中选择单元格时,我存储了indexPath.我想通过单击按钮(集合视图外部的按钮)进行更新,并将UILabel的text属性设置为按钮标题(pressedNumber).
我试过这个:
-(BOOL) calculate: (NSString *) pressedNumber{
talCell *cell = [self.collectionView
dequeueReusableCellWithReuseIdentifier:@"cell"
forIndexPath:selectedCell];
[cell.cellTitle setText:pressedNumber];
[self.collectionView reloadItemsAtIndexPaths:@[selectedCell]];
Run Code Online (Sandbox Code Playgroud)
调试时我可以看到cell.cellTitle.text属性与pressedNumber相同.感谢任何可能导致我获得正确代码的建议.
在我的应用程序中,我将使用集合视图,但我有问题.我在故事板中设计了集合视图.我在视图控制器中插入了一个集合视图,并将委托和数据源连接CategoryViewController.m到必须管理此视图控制器的类().我创建了一个非常简单的单元格,我在其中放置了一个图像视图,然后我创建了一个带有子类的类UICollectionViewCell,然后我选择了管理单元格的类,并将图像视图连接到此类.在CategoryViewController.m我设置一个数组,其中有我想要显示的图像和我用UICollectionView的委托方法,当我运行的应用程序,它说我:unrecognized selector sent to instance.我会在这里放置代码,以便您可以帮我修复它:
用于创建图像数组的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIImage *image1 = [UIImage imageNamed:@"image_glasses.png"];
UIImage *image2 = [UIImage imageNamed:@"image_jacket.png"];
UIImage *image3 = [UIImage imageNamed:@"image_pants.png"];
UIImage *image4 = [UIImage imageNamed:@"image_scent.png"];
imageArray = [NSArray arrayWithObjects:image1, image2, image3, image4, nil];
}
Run Code Online (Sandbox Code Playgroud)
委托UICollectionView的方法:
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return imageArray.count;
}
- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
UIImage *image …Run Code Online (Sandbox Code Playgroud) 是否可以创建圆形的自定义UICollectionViewCell?
为什么我需要这个?
我正在尝试创建自定义颜色选择器.我想在每个CollectionViewCell中添加一些默认颜色.这些颜色是动态的.这意味着我将改变填充细胞的颜色.
我试过的
1.我在单元格中保留了圆形png图像.但如果我想改变颜色,将不得不改变图像.
我把圆形图像保持透明(在圆形区域内).然后我改变了细胞的背景颜色.因为我将改变collectionView的背景图像而失败.
除了边界外,我保持圆形图像完全透明.然后我改变了细胞的背景颜色.失败了...
救救我..
objective-c uiview ios uicollectionview uicollectionviewcell
我有一个,UICollectionViewController并且此集合的单元格有一个子视图-假设A-与单元格边界相同。
问题是当我点击这些单元格时,collectionView:didSelectItemAtIndexPath不会被调用。我还将A的ExclusiveTouch设置为YES,但仍然没有执行任何操作。而且,当我将A的框架更改为较小的尺寸时,点击剩余区域会触发效果collectionView:didSelectItemAtIndexPath,这没问题。
那时候我应该怎么做才能使这个collectionView委托在A的界限与单元格相同的地方被调用。
我将不胜感激。
objective-c ios uicollectionview uicollectionviewcell uicollectionviewdelegate
没有关于这个主题的其他问题对我有用,所以这里就是这个.
尽管如此,我还没有出现细胞,我是以编程方式呈现UICollectionViewController来自UIViewController故事板中的细胞
注意:@IBActionsegue只是一个按钮动作
这是UIViewController:
@IBAction func segue(sender: AnyObject) {
let collectionview = CollectionViewController(collectionViewLayout: UICollectionViewLayout())
self.presentViewController(collectionview, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
那就是UICollectionViewController:
import UIKit
private let reuseIdentifier = "Cell"
class CollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.backgroundColor = UIColor.brownColor()
self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
}
// MARK: UICollectionViewDataSource
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10 …Run Code Online (Sandbox Code Playgroud) 我有两个问题。
我想知道为什么我的集合视图无需调用即可自动加载数据imageCollectionView.reloadData()。
解决了。查看评论
为什么func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize不叫?我没有看到print("collectionViewLayout called")我正在尝试修改单元格的大小,因此单元格的高度等于集合视图的高度
解决了。查看评论
这是代码
class ProductInternalDetailVC: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
var selectedProduct: Product?
@IBOutlet weak var imageCollectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
imageCollectionView.delegate = self
imageCollectionView.dataSource = self
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 2
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath as …Run Code Online (Sandbox Code Playgroud) 我使用Interface Builder 创建了一个非常简单UICollectionView的自定义文件UICollectionViewCell。我在GitHub上做了一个项目,因此您可以重现该问题。在UICollectionView模拟器上工作正常,但Xcode中不断报告错误:
“ DefaultCell预期:高度= 34,实际:高度= 150”。
Interface Builder的建议解决方案不能解决该错误。当我将UICollectionViewCell的类名更改为其他名称时,该问题有时会消失,但这只是暂时的。View Inspector或开发人员控制台中未显示其他错误。由于Interface Builder不知道单元格的正确大小,因此向单元格添加标签或图像变得困难。
请参阅下图以了解错误。
xcode ios uistoryboard uicollectionview uicollectionviewcell
ios ×9
swift ×4
objective-c ×3
selector ×2
ios6 ×1
reloaddata ×1
swift3 ×1
uiimage ×1
uilabel ×1
uistoryboard ×1
uiview ×1
xcode ×1