ios - 点击时更改 UICollectionView 单元格的背景

标签 ios objective-c uicollectionview uicollectionviewcell uicollectionviewdelegate

我有一个以编程方式创建的 UICollectionView。我希望 Collection View 以下列方式运行:

1. User touches cell
2. Cell background color changes
3. User releases touch
4. Cell background color changes

这应该是一个快速的颜色变化,发生在与点击 Action 相关的选择器执行之前,其中包含 Collection View 的 View Controller 从堆栈中弹出。

我一直在看这个问题:UICollectionView cell change background while tap

其中总结了以下用于此目的的方法:

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

我假设我只需要从“触摸开始时”和“触摸结束时”实现上述方法之一。但无论我做什么,背景颜色似乎都会发生变化,然后保持不变。这是我尝试过但没有奏效的示例:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
 {
   //pop vc 
 }

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

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

这导致单元格背景颜色仅更改为红色。我也看了这个问题:UICollectionView Select and Deselect issue并尝试实现 [UICollectionView selectItemAtIndexPath:animated:scrollPosition:] 并在 didSelectItemAtIndexPath 中调用它,但这也不起作用。 Collection View 数据源和委托(delegate)已设置。

最佳答案

问题是您要在突出显示 时更改颜色,然后在取消选择 时更改颜色,而不是在取消突出显示 时更改颜色

你应该简单地改变这个:

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

为此:

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

此外,如果您不想在突出显示之前稍等片刻,您应该将 Collection View 的 delaysContentTouches 属性设置为 NO

编辑:还要确保你打电话

[collectionView deselectItemAtIndexPath:indexPath animated:NO];

在 -didSelectItemAtIndexPath 方法中

关于ios - 点击时更改 UICollectionView 单元格的背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23392485/

相关文章:

ios - 了解大中央调度

ios - 在屏幕上设置键盘

objective-c - 在 Objective-C 应用程序中保留 session ID

ios - 为什么我的 NSNotificationObserver 在消息运行时被释放?

objective-c - 如何在 Cocoa/Mac 应用程序开发中做 UIView 风格的动画

ios - 如何将 CollectionView 添加到 TableViewCell 并检测 CollectionView 何时完成加载单元格以请求 TableViewCell 高度更新

ios - 当 iOS 应用程序在 Xamarin 中作为后台时,如何通知我蓝牙连接?

ios - 如果我在 View Controller 中为 View 编写触摸事件会怎样?

ios - 第二个 UICollectionView 不重新加载

swift - UICollectionView 水平滚动与静态焦点项目