我有一个以编程方式创建的UICollectionView.我希望集合视图以下列方式运行:
1. User touches cell
2. Cell background color changes
3. User releases touch
4. Cell background color changes
Run Code Online (Sandbox Code Playgroud)
这应该是一个快速的颜色更改,它发生在与执行tap操作相关的选择器之前,其中包含集合视图的viewcontroller从堆栈中弹出.
我一直在看这个问题:UICollectionView单元格改变背景,同时点击
其中有以下用于此目的的方法摘要:
// Methods for notification of selection/deselection and highlight/unhighlight events.
// The sequence of calls leading to selection from a user touch is:
//
// (when the touch begins)
// 1. -collectionView:shouldHighlightItemAtIndexPath:
// 2. -collectionView:didHighlightItemAtIndexPath:
//
// (when the touch lifts)
// 3. -collectionView:shouldSelectItemAtIndexPath: or - collectionView:shouldDeselectItemAtIndexPath:
// 4. -collectionView:didSelectItemAtIndexPath: or -collectionView:didDeselectItemAtIndexPath:
// 5. -collectionView:didUnhighlightItemAtIndexPath:
Run Code Online (Sandbox Code Playgroud)
我假设我只需要从"触摸开始时"和"触摸结束时"实现上述方法之一.但无论我做什么,似乎背景颜色会发生变化,然后仍然会发生变化.这是我尝试过的一些不起作用的例子:
- (void)collectionView:(UICollectionView …Run Code Online (Sandbox Code Playgroud) objective-c ios uicollectionview uicollectionviewcell uicollectionviewdelegate
目前我正在尝试创建一个UICollectionView,应该显示一个简单的excel-like-spreadsheet,用rows和columns.UICollectionViews我相信,这应该是一件容易的事.而且我真的想要实现UICollectionView,而不是任何grid framework.
但此刻我暂停了一下.我已经想通了,我好遗憾的是不能使用UICollectionViewDelegateFlowLayout的,因为这仅支持scrolling在任一 horizontally或vertically方向.但我需要scrolling双向.
因此我必须使用一个UICollectionViewLayout,但为此我没有找到好的例子,如何使用它.你有没有一个例子,如何子类化UICollectionViewLayout支持rows和columns?
提前致谢.
我正在实现自定义流布局.它有两种主要的覆盖方法来确定细胞的位置:layoutAttributesForElementsInRect和layoutAttributesForItemAtIndexPath.
在我的代码中,layoutAttributesForElementsInRect被称为,但layoutAttributesForItemAtIndexPath不是.什么决定了什么叫?layoutAttributesForItemAtIndexPath被叫哪里?
objective-c ios uicollectionview uicollectionviewlayout uicollectionviewdelegate
预设,
我有collectionViewFlowLayout子类
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
return YES;
}
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
NSArray *arr = [super layoutAttributesForElementsInRect:rect];
BBLog(@"ARRA:%@", arr);
for (UICollectionViewLayoutAttributes *attr in arr) {
if (CGAffineTransformIsIdentity(attr.transform)) {
attr.transform = CGAffineTransformMakeRotation((CGFloat)M_PI);
}
}
return arr;
}
Run Code Online (Sandbox Code Playgroud)
CollectionView旋转到倒置滚动
self.collectionView.transform = CGAffineTransformMakeRotation((CGFloat)M_PI);
Run Code Online (Sandbox Code Playgroud)
但即使jus使用本机collectionViewFlowLayout而没有子类化,也就是git这个错误
问题
我在聊天中有两条消息,但是当在底部滚动(通常是顶部)消失第二项时.
给定rect的layoutAttributesForElementsInRect为两个indexPaths 0-0和0-1返回两个属性,但委托方法
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)
仅调用indexPath 0-0
这里的图片
更新 所以我发现为什么会发生 - 这个行代码
attr.transform = CGAffineTransformMakeRotation((CGFloat)M_PI);
Run Code Online (Sandbox Code Playgroud)
看看是否删除转换
ios uicollectionview uicollectionviewcell uicollectionviewlayout uicollectionviewdelegate
我有一个UITableview.其中一个UITableViewCell's是UICollectionview实例名称"amenityView".的UICollectionViewDelegate和UICollectionViewDataSource在所设定的storyboard下面,如图所示.调用以下方法并按预期填充数据.
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
- (UICollectionViewCell *)collectionView:(UICollectionView *)collection cellForItemAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)

但是,当我选择UICollectionViewCell包含在中的包含时,下面的方法没有被调用UICollectionView.我错过了什么?
-(void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
-(void) collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)
UPDATE:在此方法中返回YES collectionView:shouldSelectItemAtIndexPath:将调用以下两个方法.至少那是我所缺少的.希望这会对一些身体有所帮助......

uitableview uicollectionview uicollectionviewcell uicollectionviewdelegate
我没有调用任何UICollectionViewDelegateFlowLayout方法.有人可以帮我找到解决方案吗?
在我的ViewDidLoad方法中,我为集合视图设置了委托和数据源as
self.collectionView.delegate = self和
self.collectionView.dataSource = self
这是我的委托方法:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
let deviceIdiom = UIScreen.mainScreen().traitCollection.userInterfaceIdiom
switch(deviceIdiom){
case .Phone:
return UIEdgeInsets(top: 50, left: 10, bottom: 0, right: 10)
case .Pad:
return UIEdgeInsets(top: 60, left: 20, bottom: 0, right: 20)
default:
break
}
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return CGFloat(8)
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { …Run Code Online (Sandbox Code Playgroud) 如何仅为一个部分显示页脚视图?
正如我发现的那样,我无法通过nil隐藏其他部分的页脚视图,因为它会导致崩溃.
override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
var v : UICollectionReusableView! = nil
if kind == UICollectionElementKindSectionHeader {
let x = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier:reuseHeaderIdentifier, forIndexPath:indexPath) as HouseNameReusableView
let h = houses[indexPath.section]
x.nameLabel.text = h["name"] as? String
return x
}else if kind == UICollectionElementKindSectionFooter {
if indexPath.section == houses.count - 1{
let x = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionFooter, withReuseIdentifier:reuseFooterIdentifier, forIndexPath:indexPath) as FooterCollectionReusableView
return x
}
}
return v
}
Run Code Online (Sandbox Code Playgroud) 我UICollectionView在我的 swift 类中使用它,它放在我的UIViewController. 我在我的代码中将 collectionView 连接到插座,我进行了设置,delegate然后datasource在我的应用程序中看到了结果。除了当我单击每个单元格时一切正常之外,什么都没有发生。
我的代码如下:
class UsersList: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet weak var tview: UICollectionView!
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
tview.backgroundColor = UIColor.whiteColor() //this works
tview.delegate = self
tview.dataSource = self
}
func collectionView(tview: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
print("You selected cell #\(indexPath.item)!")
//this does not appear in the console :(
}
Run Code Online (Sandbox Code Playgroud)
我还能做些什么来在控制台中看到打印消息?
ios uicollectionview uicollectionviewcell uicollectionviewdelegate swift
我最近开始了另一个项目,对 Swift 进行了一些探索。我想使用 NSFetchedResultsController 实现 Collection View 以从 CoreData 数据库中获取数据。我想使用https://github.com/AshFurrow/UICollectionView-NSFetchedResultsController中的示例 并在 Swift 中实现类似的东西。我不需要任何移动事件,因此我实现了以下操作:
首先,我创建了一个类来保存所做的更改:
class ChangeItem{
var index:NSIndexPath
var type:NSFetchedResultsChangeType
init(index: NSIndexPath, type: NSFetchedResultsChangeType){
self.index = index
self.type = type
}
}
Run Code Online (Sandbox Code Playgroud)
在我的集合视图控制器中,我使用两个数组来临时保存更改
var sectionChanges:[ChangeItem] = []
var objectChanges:[ChangeItem] = []
Run Code Online (Sandbox Code Playgroud)
然后我等待 FetchedResultsController 在 CoreData 数据库发生更改后更改某些内容
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?,forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
var item:ChangeItem?
if(type == NSFetchedResultsChangeType.Insert){
item = ChangeItem(index: newIndexPath!, type: type)
}else if(type == NSFetchedResultsChangeType.Delete){
item = ChangeItem(index: …Run Code Online (Sandbox Code Playgroud) nsfetchedresultscontroller ios uicollectionview uicollectionviewdelegate swift
insetForSectionAtIndex(在DelegateFlowLayout上)使您可以为节中的所有单元格设置插入
sectionInset(在FlowLayout上)使您可以设置适用于所有部分的插图。
但是,我正在寻找一种仅将插页应用于一个特定部分的方法-这可能吗?
ios uicollectionview uicollectionviewlayout uicollectionviewdelegate swift