简体   繁体   English

UITableViewCell 删除滑动动画丑陋

[英]UITableViewCell delete on swipe animation ugly

I have the following problem.我有以下问题。 When I delete UITableViewCells from my UITableView with "swipe to delete" I see an ugly behaviour of the UITableView.当我使用“滑动删除”从我的 UITableView 中删除 UITableViewCells 时,我看到了 UITableView 的丑陋行为。 Regardless which animation I choose in the deleteRows method all deletions result in the same strange behaviour.无论我在 deleteRows 方法中选择哪种动画,所有删除都会导致相同的奇怪行为。

This is my code for deleting the cell这是我删除单元格的代码

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        print("Deleted")

        self.tvItems.beginUpdates()

        if(indexPath.section == 0){
            Shoppingcart.shared.removeItemForId(ident: indexPath.row)
        }else if (indexPath.section == 1){
            Shoppingcart.shared.serviceContent.remove(at: indexPath.row)
        }

        if let parent = self.parent as? CollectionViewController{
            parent.collectionView?.reloadData()
        }
        self.tvItems.deleteRows(at: [indexPath], with: .fade)

        self.tvItems.endUpdates()

    }
}  

Here is an image of the broken animation:这是损坏的动画的图像:

在此处查看损坏的动画

I found a solution for my problem.我找到了解决我的问题的方法。 The tableviewcell hight estimation was set on automatic. tableviewcell 高度估计设置为自动。 I did set the hight on the same as the row height and the broken animation disappeared.我确实将高度设置为与行高相同,并且损坏的动画消失了。

Settings for the broken animation:破碎动画的设置:

错

Settings for the working animation:工作动画的设置:

对

I had A LOT of trouble with this.我在这方面遇到了很多麻烦。 Moving completion(true) to the top instead of at the bottom solved the problem.将完成(真)移动到顶部而不是底部解决了问题。 Hope it helps someone out there!希望它可以帮助那里的人!

    alert.addAction(UIAlertAction(title: "Delete", style: .destructive) { (action) in
        completion(true) // *** PUT THIS HERE!!! ****
        self.searchFoodView.beginUpdates()
        self.foodMachine.deleteFood(at: indexPath.row - 1)
        self.searchFoodView.deleteRows(at: [indexPath], with: .fade)
        self.searchFoodView.endUpdates()
    })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM