Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UICollectionView单元格轻按时更改背景_Ios_Objective C_Uicollectionview_Uicollectionviewdelegate - Fatal编程技术网

Ios UICollectionView单元格轻按时更改背景

Ios UICollectionView单元格轻按时更改背景,ios,objective-c,uicollectionview,uicollectionviewdelegate,Ios,Objective C,Uicollectionview,Uicollectionviewdelegate,只有在点击元素时才有可能更改UICollectionView的背景色。我试过: -(void) collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath{ //change color when tapped } -(void)collectionView:(UICollectionView *)collectionView didUnhi

只有在点击元素时才有可能更改
UICollectionView
的背景色。我试过:

-(void) collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath{
    //change color when tapped
}

-(void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath{
    //change back on touch up 
}
但结果是,只有当我的手指保持更长的时间时,我才能看到变化。 是否存在类似于
UITableViewCell
方法
willSelectItemAtIndexPath:
中的内容

但结果是,只有当我的手指保持更长的时间时,我才能看到变化

您遇到的延迟可能与情节提要中的“延迟内容接触”复选框有关


请尝试取消选中。

我想您可能需要将所选单元格保留为不同的背景色,对吗? 然后试试这个代码

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor magentaColor];
}

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor cyanColor];
}
只需为处于不同状态的单元分配不同的BG颜色即可。 此外,下面的代码是当有人触摸collectionView单元格时序列触发方法的文档。您还可以在UICollectionView.h文件UICollectionViewDelegate协议部分中找到这些文档

// 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:

我一直在寻找这个答案。。。我正在将自定义网格更改为UICollectionView和TableView组合视图。实现这两种方法允许选择然后更改选择。谢谢Steve,这帮我搞定了。当你滚动收藏视图时,这个方法不起作用。重复使用的单元格也会着色。应使用和上述问题中的
didHighlightItemAtIndexPath
didUnhighlightItemAtIndexPath
// In Swift    
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
  let cell = collectionView.cellForItemAtIndexPath(indexPath) as! UICollectionViewCell
  cell.backgroundColor = UIColor.magentaColor()
}

func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
  let cell = collectionView.cellForItemAtIndexPath(indexPath) as! UICollectionViewCell
  cell.backgroundColor = UIColor.cyanColor()
}