【问题标题】:How to check if a cell has been displayed Swift如何检查单元格是否已显示 Swift
【发布时间】:2016-07-21 09:55:44
【问题描述】:

当我显示我的表格视图时,我让我的单元格带有这样的动画。

 func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

             cell.alpha = 0
             let rotation = CATransform3DTranslate(CATransform3DIdentity, -250, 20, 0)
             cell.layer.transform = rotation
             UIView.animateWithDuration(0.9){
             cell.alpha = 1
             cell.layer.transform = CATransform3DIdentity

    }

它是这样工作的,但是当我向下然后再次向上查看之前的单元格时,动画仍然存在。

显示我相信我必须检查单元格是否已显示。

我尝试使用 didEndDisplayingCell 函数并将 cell.tag 放入数组中,然后我使用 array.contains(cell.tag) 检查当前单元格是否在该数组中,但它不起作用。 实际上动画只对前三个单元有效,然后什么都没有。

【问题讨论】:

  • 使用 visibleCells 获取可见单元格。
  • @Anbu.Karthik 感谢您的回答,但我想知道已看到哪些单元格,因此动画不会再次用于已看到的单元格
  • 将单元格 indexPath 添加到您的数组而不是标签中,这些单元格正在被重复使用,因此重复使用的单元格的标签将相同。

标签: ios swift uitableview cell


【解决方案1】:

您应该保留一个 indexPaths 数组,并在显示单元格时添加到其中。这样你就可以检查单元格是否已经动画了。

if (!indexPaths.contains(indexPath)) {
    indexPaths.append(indexPath)
    cell.alpha = 0
    let rotation = CATransform3DTranslate(CATransform3DIdentity, -250, 20, 0)
    cell.layer.transform = rotation
    UIView.animateWithDuration(0.9){
        cell.alpha = 1
        cell.layer.transform = CATransform3DIdentity
    }
}

【讨论】:

  • 谢谢您.. 成功了!我会在 3 分钟内接受你的回答
【解决方案2】:

正如 @James-P 在 cmets 中提到的 - 只需创建一个数组

var indexPathArray = [NSIndexPath]()

并重写你的 willDisplayCell 方法:

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

    if !indexPathArray.contains(indexPath) {

        indexPathArray.append(indexPath)

        cell.alpha = 0
        let rotation = CATransform3DTranslate(CATransform3DIdentity, -250, 20, 0)
        cell.layer.transform = rotation
        UIView.animateWithDuration(0.9){
            cell.alpha = 1
            cell.layer.transform = CATransform3DIdentity
        }
    }
}

【讨论】:

    【解决方案3】:

    您可以做的是将单元格的索引路径放入数组中并检查索引路径是否在数组中。如果索引路径在数组中则不执行动画

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多