问题描述
所以我使用自定义动画来显示表格单元格的显示方式。它们有点淡入并稍微向上滑动。我正在使用来自 API 调用的数据填充表。
我注意到它并不总是发生。有时它会按预期工作,有时单元格/数据只是突然出现,没有动画/移动。他们只是在动画结束时突然出现。我真的没有看到它何时起作用和何时不起作用的任何一致模式。看起来很随意。
知道是什么导致了这种行为吗?
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell: RecentActivityCell = recentActivityView.dequeueReusableCell(withIdentifier: "cellId",for: indexPath) as! RecentActivityCell
var lastinitialdisplayableCell = false
//change flag as soon as last displayable cell is being loaded (which will mean table has initially loaded)
if userActivityArray.count > 0 && !finishedLoadingInitialTableCells {
if let indexPathsForVisibleRows = tableView.indexPathsForVisibleRows,let lastIndexPath = indexPathsForVisibleRows.last,lastIndexPath.row == indexPath.row {
lastinitialdisplayableCell = true
}
}
if !finishedLoadingInitialTableCells {
if lastinitialdisplayableCell {
finishedLoadingInitialTableCells = true
}
//animates the cell as it is being displayed for the first time
cell.transform = CGAffineTransform(translationX: 0,y: 40/2)
cell.alpha = 0
UIView.animate(withDuration: 0.5,delay: 0.05*Double(indexPath.row),options: [.curveEaseInOut],animations: {
cell.transform = CGAffineTransform(translationX: 0,y: 0)
cell.alpha = 1
},completion: nil)
}
cell.backgroundColor = UIColor.clear
if userActivityArray[indexPath.row].status == "Success" {
cell.statusImage.image = UIImage(named: "passImage")
} else {
cell.statusImage.image = UIImage(named: "failImage")
}
cell.activity = userActivityArray[indexPath.row]
return cell
}
解决方法
尝试通过覆盖在“yourTableViewCell”中使用您的动画:
override func prepareForReuse() {
super.prepareForReuse()
\\ Animation code over here
}
不要在“CellForRowAt”中使用