重新排序期间拖动时更改 UITableViewCell 的背景视图

问题描述 投票:0回答:1

当用户重新排序其项目并拖动单元格时,我希望单元格具有薄的材料作为背景。但是,当用户没有拖动任何单元格时,背景将是清晰的,以允许表格视图背景视图穿过单元格。

我无法让它工作......

func tableView(_ tableView: UITableView, dragPreviewParametersForRowAt indexPath: IndexPath) -> UIDragPreviewParameters? {
        // Create a copy of the cell for drag preview
        guard let cell = tableView.cellForRow(at: indexPath) else { return nil }
        
        // Create a visual effect view with blur effect
        let blurEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .systemThinMaterial))
        blurEffectView.frame = cell.bounds
        
        // Create a preview parameters object
        let previewParameters = UIDragPreviewParameters()
        let previewView = UIView(frame: cell.bounds)
        previewView.addSubview(blurEffectView)
        previewParameters.visiblePath = UIBezierPath(roundedRect: previewView.bounds, cornerRadius: 0)
        previewParameters.backgroundColor = .clear
        
        return previewParameters
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        cell.backgroundColor = .clear
        let item = fetchedResultsController.object(at: indexPath)
        cell.contentConfiguration = UIHostingConfiguration {
            ItemRow(item: item)
        }
        return cell
    }
swift uitableview swiftui uikit
1个回答
0
投票

简而言之:您必须创建 UITableViewCell 的自定义子类。重写layoutSubviews以将UILongPressGestureRecognizer附加到UITableViewCellReorderControl。定义一个协议并使用委托来通知任何您想要的人有关拖动状态的信息。

CustomTableViewCell.swift:

   import UIKit

// Define the protocol in Swift
protocol CustomTableViewCellDelegate: AnyObject {
    func customTableViewCell(_ cell: CustomTableViewCell, isDragging value: Bool)
}

// Create the custom table view cell
class CustomTableViewCell: UITableViewCell {

    // Declare the delegate property
    weak var delegate: CustomTableViewCellDelegate?

    // Initialize the cell (if needed)
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        // Initialization code
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        // Initialization code
    }

    // Example method to notify delegate
    func someActionThatTriggersDragging() {
        delegate?.customTableViewCell(self, isDragging: true)
    }
}

CustomTableViewCell.swift:

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, CustomTableViewCellDelegate {

    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(CustomTableViewCell.self, forCellReuseIdentifier: "CustomTableViewCell")
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10 // Example row count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as? CustomTableViewCell else {
            return UITableViewCell()
        }
        cell.delegate = self
        return cell
    }

    func customTableViewCell(_ cell: CustomTableViewCell, isDragging value: Bool) {
        // Handle the delegate callback
        print("Cell is dragging: \(value)")
       // Here you may change the color or background of the cell that is being dragged
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.