简体   繁体   中英

Shadow doesn't stay during updates of UITableView

I am using tableView.beginUpdates() and tableView.endUpdates() to expand/contract a cell that has shadow. When the table updates, it removes all shadows from cells and then puts them back as shown here

I have tried using willDisplayCell and also tried changing the shadow to its own view, shadow on contentView and shadow on cell, none worked.

How do i keep the shadow?

extension UIView {
    func addTutShadow(shadowOpacity: Float? = nil) {
        self.layer.shadowColor = UIColor.black.cgColor
        self.layer.shadowOffset = CGSize(width: 0, height: 3)
        self.layer.shadowRadius = 12 * kHeightFactor
        self.layer.shadowOpacity = shadowOpacity ?? 0.12
    }
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = 
    tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TopicListCard
    cell.topic = rankedTopics[indexPath.section]
    cell.backgroundColor = .clear
    cell.backgroundView = UIView()
    cell.selectedBackgroundView = UIView()
    cell.delegate = self
    cell.addTutShadow()
    cell.setup()
    return cell
}

var selectedIndex = -1
    
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if selectedIndex != indexPath.section {
        selectedIndex = indexPath.section
        tableView.beginUpdates()
        tableView.endUpdates()
    } else {
        selectedIndex = -1
        tableview.deselectRow(at: indexPath, animated: true)
        tableView.beginUpdates()
        tableView.endUpdates()
    }
        
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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