标签: uicollectionviewcell

首次加载后如何在UICollectionView中选择一些项目?

我试着写

[self collectionView:myCollectionView didSelectItemAtIndexPath:selectedIndexPath]

和viewDidLoad中的UICollectionViewCell选择= YES,它确实实现了该方法didSelectItemAtIndexPath,但未选择单元格.

我在UICollectionViewCell子类中编写了所选状态(void)setSelected:(BOOL)selected.加载视图后,手动选择功能起作用.但是在视图首次加载后我无法自动选择一些项目.

我试着写代码:

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

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath,一切都不行.

我发现它第一次运行viewDidLoaddidSelectItemAtIndexPath,然后cellForItemAtIndexPath,好像,我无法让细胞在indexPath(我知道)之前cellForItemAtIndexPath,因为在此之前,该小区是不存在的.那么如何在UICollectionView首次加载后选择一些项?

抱歉我的英语不好.提前致谢.

xcode ios uicollectionview uicollectionviewcell

13
推荐指数
3
解决办法
2万
查看次数

在UITextView上点击时,未调用UICollectionView didSelectItemAtIndexPath

我有UICollectionView自定义单元格 - 它们UITextView主要覆盖整个单元格.这在使用时出现问题didSelectItemAtIndexPath.触发它的唯一方法是在外面敲击UITextView.我想让它在您点击的单元格中的任何位置触发,无论是否有文本视图.如何才能做到这一点?

ios uicollectionview uicollectionviewcell ios7

13
推荐指数
2
解决办法
2万
查看次数

NSIndexPath不会将项目,部分或行识别为属性

当iOS 7.1发送我的viewController时:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView 
    cellForItemAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)

indexPath对象无法识别属性:item,section或row.期望值是section = 0,item = 0

调试器显示:

**indexPath NSIndexPath *   0xc000000000000016

 NSObject       
_indexes    NSUInteger *    NULL    
  *_indexes     
_length     
_reserved   void *  NULL
Run Code Online (Sandbox Code Playgroud)

日志报告:

(lldb) po indexPath
<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}
(lldb) po indexPath.item
error: property 'item' not found on object of type 'NSIndexPath *'
error: 1 errors parsing expression
(lldb) po indexPath.row
error: property 'row' not found on object of type 'NSIndexPath *'
error: 1 errors parsing …
Run Code Online (Sandbox Code Playgroud)

nsindexpath ios uicollectionview uicollectionviewcell

13
推荐指数
2
解决办法
9226
查看次数

使用iOS 8 SDK构建时,iOS 7上的UICollectionViewCell大小调整问题:这是一个错误吗?

我开始为iOS 8构建我的iOS应用程序,并且我很快发现UICollectionViewCell存在问题,无论您使用自定义集合布局与否.许多细胞没有正确显示,内容被切断等.

快速修复:将以下自动调整遮罩添加到单元格的内容视图中:

self.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
Run Code Online (Sandbox Code Playgroud)

当我使用iOS 7 SDK构建时,这种情况不会发生.使用iOS 8 SDK构建并在iOS 8上运行时也不会发生这种情况.

这是一个bug还是iOS 8之后还有什么不同的地方?

uikit uicollectionview uicollectionviewcell ios7 ios8

13
推荐指数
1
解决办法
834
查看次数

在Xcode 10 beta上运行后,UICtackView内部出现UICollectionViewCell autolayout问题

我的堆栈视图包含一个图像和标签,用于显示某些任务.它们都在多行上保持对齐.

我没有在Xcode 9上运行任何问题,但是当我在Xcode 10 beta 6上运行时,我遇到了一些自动布局问题,我总是会遇到堆栈视图错误:

Need constraint for X position
Need constraint for Y position
Run Code Online (Sandbox Code Playgroud)

我目前对堆栈视图的约束是:

Leading edge to cell - 5
Trailing edge to cell - 5
Bottom edge to cell - 2
Top edge to cell - 2
Run Code Online (Sandbox Code Playgroud)

所以我没有看到有什么改变抱怨这个,对我来说很明显我已经设置了X和Y.

这是两个截图,显示它运行时的标签(标签被截断)和故事板:

在此输入图像描述

在此输入图像描述

ios uicollectionviewcell swift uistackview xcode10

13
推荐指数
1
解决办法
1972
查看次数

在Tap上动画UICollectionViewCell

UICollectionViewCell当用户点击一个单元格时,我想开始一些动画.我的想法是选择相应的单元格didSelectItemAtIndexPath并触发动画.但是,这不起作用:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
// animate the cell user tapped on
ProductCollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ProductReuseID" forIndexPath:indexPath];

[UIView animateWithDuration:5.0
                      delay:0
                    options:(UIViewAnimationOptionAllowUserInteraction)
                 animations:^{
                     NSLog(@"animation start");
                     [cell.layer setBackgroundColor:[UIColor colorWithRed: 180.0/255.0 green: 238.0/255.0 blue:180.0/255.0 alpha: 1.0].CGColor];
                 }
                 completion:^(BOOL finished){
                     NSLog(@"animation end");
                     [cell.layer setBackgroundColor:[UIColor whiteColor].CGColor];
                 }
 ];
}
Run Code Online (Sandbox Code Playgroud)

实际上,动画同时开始和结束(尽管animateWithDuration设置为5).下一次尝试是跳过动画并简单地设置一个不同的边框样式:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
// animate the cell user tapped on
ProductCollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ProductReuseID" forIndexPath:indexPath];

[cell.layer setBorderWidth:5.0f];
}
Run Code Online (Sandbox Code Playgroud)

但是,这并没有改变任何东西(可能是因为我必须手动重绘单元格?).

你有任何想法如何在用户点击它时动画UICollectionViewCell吗?

亲切的问候,基督徒

core-animation ios6 uicollectionview uicollectionviewcell

12
推荐指数
1
解决办法
1万
查看次数

UICollectionView:不会对重用项目计算不同大小的项目

我有一个集合视图,我声明了不同的项目大小

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

    FeedItem *item = [_imagesLinkArray objectAtIndex:indexPath.row];
    return CGSizeMake(250, 200*item.sizeFactor);
}
Run Code Online (Sandbox Code Playgroud)

当正在重复使用单元格时,正在使用已重新collectionView:cellForItemAtIndexPath: 计划的项目大小而不是指定的项目大小来呈现该单元格sizeForItemAtIndexPath:.

有任何想法吗?

ios uicollectionview uicollectionviewcell

12
推荐指数
2
解决办法
1万
查看次数

在单元格上设置角半径会导致UICollectionView的性能下降

我有一个UICollectionView只有几个细胞(约20).这个系列的性能非常好.但是,一旦我尝试围绕UICollectionViewCells这个视图呈现的角落,我的表现就会受到重创.在我的cell的init方法中,这是我添加的唯一一行导致这个:

[self.layer setCornerRadius:15];
Run Code Online (Sandbox Code Playgroud)

由于这是在init方法中,我正在重新使用单元格,我不明白为什么这应该导致我的问题.

我尝试使用以下多种组合来调整销售的光栅化和不透明度,但仍然没有效果:

[self.layer setMasksToBounds:YES];
[self.layer setCornerRadius:15];
[self.layer setRasterizationScale:[[UIScreen mainScreen] scale]];
self.layer.shouldRasterize = YES;
self.layer.opaque = YES;
Run Code Online (Sandbox Code Playgroud)

他们的一些设置或技巧是为了改善UICollectionView具有圆角的单元格的性能吗?

ios uicollectionview uicollectionviewcell

12
推荐指数
1
解决办法
9761
查看次数

如何在IOS中以编程方式滚动UICollectionViewCell?

我有一个垂直UICollectionView的每个单元占用整个self.view.frame我可以轻松向上滑到页面到下一个单元格,但我想按下一个按钮做同样的事情.

我尝试过使用:

- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated
Run Code Online (Sandbox Code Playgroud)

和:

- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated
Run Code Online (Sandbox Code Playgroud)

他们工作但他们暂时"白出"当前的Cell以呈现nextCell,而刷卡在转换过程中显示两个单元格.

理想情况下我想使用:

- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated
Run Code Online (Sandbox Code Playgroud)

但我不知道如何访问nextCell indexPath...我试过:

NSIndexPath *nextIndexPath = [_collectionView indexPathForItemAtPoint:CGPointMake(25, (_collectionView.frame.size.height-25)*(_currentIndexRowForCellOnScreen+1))];
Run Code Online (Sandbox Code Playgroud)

哪里_currentIndexRowForCellOnScreenindexPath.row对的UICollectionView的屏幕上的首次亮相:

- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)

但当我把它放入:

- (NSIndexPath *)indexPathForCell:(UICollectionViewCell *)cell
Run Code Online (Sandbox Code Playgroud)

它在第一个Cell之后返回NULL,并且没有动画....

任何方向将不胜感激.感谢您的时间.

objective-c uiscrollview ios uicollectionview uicollectionviewcell

12
推荐指数
3
解决办法
2万
查看次数

UICollectionViewCell寄存器类失败,但注册nib工作

UICollectionViewCell为我创建一个自定义UICollectionViewController,它通过注册笔尖来工作; 但是,没有按班级注册.

使用registerClass() - 失败

按类注册似乎是正确的 - 它构建,但在运行时抛出异常,从UIView中解开可选元素.没有引用插座,它运行; 但是,集合视图中不会出现单元格.

collectionView?.registerClass(MyCollectionViewCell.self, 
                              forCellWithReuseIdentifier: "myCell")
Run Code Online (Sandbox Code Playgroud)

很明显,视图没有加载; 但是,我认为设置单元格的自定义类将提供必要的链接.

使用registerNib() - 工作

通过nib注册工作,这在显式加载视图时是有意义的.

let nib = UINib(nibName: "MyCollectionViewCell", bundle: nil)
collectionView?.registerNib(nib, forCellWithReuseIdentifier: "myCell")
Run Code Online (Sandbox Code Playgroud)

这里明确引用了视图,并为视图设置了自定义类; 然而,关于这一点似乎是错误的.

示例项目

隔离示例项目中的问题,下面是要复制的视图和代码; 或者,这个项目可以在GitHub上找到.

Main.storyboard

我的主要故事板初始视图控制器是一个UICollectionViewController子类MyCollectionViewController:

UICollectionViewController

MyCollectionViewController.swift

显示由笔尖和按类注册:

import UIKit

class MyCollectionViewController: UICollectionViewController {

    var data = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

        data = ["a", "b", "c"]

        // This works, by nib
        let nib = UINib(nibName: "MyCollectionViewCell", bundle: nil)
        collectionView?.registerNib(nib, forCellWithReuseIdentifier: …
Run Code Online (Sandbox Code Playgroud)

uicollectionview uicollectionviewcell swift

12
推荐指数
1
解决办法
1万
查看次数