簡體   English   中英

重新加載數據后,UITableView中的UIView動畫不起作用

[英]UIView animation in UITableView doesn't work after reload data

我現在在我的應用程序中遇到一個奇怪的錯誤。 我有一個UIView擴展,可將指示器向左右旋轉90度。 我正在使用UITableViewController作為導航抽屜。 我還添加了一個可擴展列表,並將UITableViewHeaderFooterView用於主要區域(具有子項)。

UITableViewHeaderFooterView就像主要的“單元格”一樣。 在它們下面是UITableView的真實單元格,當用戶點擊UITableViewHeaderFooterView時,這些單元格將被刪除並插入。

根據WebView(主視圖)的URL,我想更新tableView的內容。 數據更改后,擴展程序的動畫將不再起作用。 該代碼仍將執行,並且值正確。 我只是看不到動畫了。

UITableViewHeaderFooterView:

import UIKit

class TableSectionHeader: UITableViewHeaderFooterView {
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var containerView: UIView!
    @IBOutlet weak var indicatorImage: UIImageView!

    override func draw(_ rect: CGRect) {
        super.draw(rect)

        indicatorImage.image = IconFont.image(fromIcon: .next, size: Constants.Sizes.MENU_INDICATOR_SIZE, color: Constants.Colors.WHITE_COLOR)
    }

    func setExpanded(expanded: Bool) {
        indicatorImage.rotate(expanded ? .pi / 2 : 0.0)
    }
}

WebViewController內部的方法:

private func loadNewMenu(href: String) {
    NetworkService.sharedInstance.getNavigation(path: href, completion: { menu in
        if let menuController = self.menuVC {
            menuController.menuItems = menu
            menuController.tableView.reloadData()
        }
    })
 }

UIView擴展:

import UIKit

extension UIView {

    func rotate(_ toValue: CGFloat, duration: CFTimeInterval = 0.2) {
        let animation = CABasicAnimation(keyPath: "transform.rotation")

        animation.toValue = toValue
        animation.duration = duration
        animation.isRemovedOnCompletion = false
        animation.fillMode = kCAFillModeForwards

        self.layer.add(animation, forKey: "rotationIndicator")
    }

}

MenuTableViewController的相關方法:

// MARK: Custom Menu Methods

@objc func handleExpandClose(sender: UITapGestureRecognizer) {        
    guard let section = sender.view?.tag else {
        return
    }

    var indexPaths = [IndexPath]()

    for row in subItems.indices {
        let indexPath = IndexPath(row: row, section: section)
        indexPaths.append(indexPath)
    }

    let isExpanded = menuItems[section].isExpanded
    menuItems[section].isExpanded = !isExpanded
    sectionHeaders[section].setExpanded(expanded: !isExpanded)

    if isExpanded {
        tableView.deleteRows(at: indexPaths, with: .fade)
    } else {
        tableView.beginUpdates()
        closeOpenedSections(section: section)
        tableView.insertRows(at: indexPaths, with: .fade)
        tableView.endUpdates()
    }
}

private func closeOpenedSections(section: Int) {
    var closeIndexPaths = [IndexPath]()

    for (sectionIndex, sec) in menuItems.enumerated() {
        guard let subItems = sec.subItems, sec.isExpanded, sectionIndex != section else {
            continue
        }

        sec.isExpanded = false
        for rowIndex in subItems.indices {
            let closeIndexPath = IndexPath(row: rowIndex, section: sectionIndex)
            closeIndexPaths.append(closeIndexPath)
        }

        sectionHeaders[sectionIndex].setExpanded(expanded: false)

        tableView.deleteRows(at: closeIndexPaths, with: .fade)
    }
}

那就是我的MenuTableViewController中的viewForHeaderInSection的實現:

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    if let sectionHeader = self.tableView.dequeueReusableHeaderFooterView(withIdentifier: "TableSectionHeader") as? TableSectionHeader {
        sectionHeader.setUpHeader(emphasize: menuItems[section].emphasize)
        sectionHeader.titleLabel.text = menuItems[section].title

        let tap = UITapGestureRecognizer(target: self, action: #selector(MenuTableViewController.handleExpandClose(sender:)))
        sectionHeader.addGestureRecognizer(tap)

        sectionHeader.indicatorImage.isHidden = menuItems[section].subItems != nil ? false : true
        sectionHeader.tag = section
        self.sectionHeaders.append(sectionHeader)

        return sectionHeader
    }

    return UIView()
}

我知道這是很多代碼,但是我想它與tableView的reloadData()有某種關系。 在執行重新加載之前,動畫效果非常好。 如果在重新加載菜單之前沒有打開菜單,我也看不到該錯誤。 它只是在第一次重新加載數據后發生(即使數據完全相同)。

同樣,動畫代碼仍然會執行,並且擴展值是正確的值(是/否)。 我已經在控制台上打印了在其上進行動畫處理的UIImageView,即使重新加載后,它似乎也是相同的視圖。 但是動畫沒有出現。

原來這是我自己的一個錯誤! 我正在使用兩個數組(sectionHeaders,menuItems)。 當我收到新數據時,我只更改了其中之一。 因此,單元格和我正在處理的內容之間不匹配。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM