Почему 'действия редактирования' не отображаются, но вызывается (_: editActionsForRowAt) - PullRequest
0 голосов
/ 05 августа 2020

Когда я смахиваю влево, действия редактирования не появляются. Но! Вызываются все функции delegete (canEditRowAt и editActionsForRowAt) Почему я не могу смахнуть?

import UIKit

class HikingCustomLocationViewController: UIViewController {
    
    //MARK: - Constants
    
    private let DEFAULT_HEADER_FOOTER_VIEW_ID = "DefaultHeaderFooterViewId"
    private let CUSTOM_LOCATION_CELL_ID = "CustomLocationCellId"
    
    // MARK: - UI properties
    
    private lazy var customLocationTableView: UITableView = {
        let tableView = UITableView()
        tableView.backgroundColor = ColorHelper.bckgDefaultBlue
        tableView.separatorStyle = .singleLine
        tableView.separatorColor = ColorHelper.bckgDefaultBlue
        tableView.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        tableView.indicatorStyle = .white
        tableView.backgroundColor = ColorHelper.bckgDefaultBlue
        tableView.isHidden = true
        
        // Delagates
        tableView.delegate = self
        tableView.dataSource = self

        // Registration
        let defaultHeaderFooterViewNib = UINib(nibName: "DefaultTableViewHeaderFooterView", bundle: nil)
        tableView.register(defaultHeaderFooterViewNib, forHeaderFooterViewReuseIdentifier: DEFAULT_HEADER_FOOTER_VIEW_ID)

        let customLocationViewCellNib = UINib(nibName: "PlacesTableViewCell", bundle: nil)
        tableView.register(customLocationViewCellNib, forCellReuseIdentifier: CUSTOM_LOCATION_CELL_ID)
        
        return tableView
    }()
    
    private lazy var userMessageLabel: UILabel = {
        let label = UILabel()
        label.font = FontHelper.body
        label.textAlignment = .center
        label.numberOfLines = 0
        label.textColor = ColorHelper.bckgTextTextWhite
        label.text = NSLocalizedString("PLACESNOTHINGADDED", comment: "")
        label.isHidden = true
        return label
    }()
    
    // MARK: - Dependencies
    
    private lazy var messageBus = MessageBus.sharedInstance
    private lazy var globalState = GlobalState.sharedInstance
    
    // MARK: - Props
    
    enum HikingCustomLocationProps {
        case loading
        case loaded(customLocationGroups: [(tourId: String, customLocations: [CustomLocation])])
    }
    
    private var props: HikingCustomLocationProps = .loading {
        didSet {
            view.setNeedsLayout()
        }
    }
    
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        
        switch props {
        case .loading:
            customLocationTableView.isHidden = true
            userMessageLabel.isHidden = true
        case .loaded(let customLocationGroups):
            if customLocationGroups.isEmpty {
                customLocationTableView.isHidden = true
                userMessageLabel.isHidden = false
            } else {
                customLocationTableView.isHidden = false
                userMessageLabel.isHidden = true
            }
            
            customLocationTableView.reloadData()
        }
    }
    
    // MARK: - Life cycle
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        title = NSLocalizedString("PLACESTITLE", comment: "")
        view.backgroundColor = ColorHelper.bckgDefaultBlue
        
        setupMessageBusDelegates()
        subscribeMessages()
        
        view.addSubview(customLocationTableView)
        view.addSubview(userMessageLabel)
        
        setupConstraints()
        
        if case .product(let appStoreId) = globalState.state {
            messageBus.send(msg: GetCustomLocationsAction(appStoreId: appStoreId))
        }
    }
    
    deinit {
        unsubscribeMessages()
    }
    
    // MARK: - MessageBus
    
    private var customLocationsReadyMessage: MessageBusDelegate<CustomLocationsReadyMessage>?
    
    private func setupMessageBusDelegates() {
        customLocationsReadyMessage = MessageBusDelegate<CustomLocationsReadyMessage>(closure: { [weak self] msg in
            if case .product(let appStoreId) = self?.globalState.state {
                if msg.appStoreId != appStoreId { return }
                self?.props = .loaded(customLocationGroups: msg.customLocationsGroup)
            }
            
            if case .tour(let appStoreId, _, _) = self?.globalState.state {
                if msg.appStoreId != appStoreId { return }
                self?.props = .loaded(customLocationGroups: msg.customLocationsGroup)
            }
        })
    }
    
    private func subscribeMessages() {
        messageBus.subscribe(closure: customLocationsReadyMessage)
    }
    
    private func unsubscribeMessages() {
        messageBus.unsubscribe(closure: customLocationsReadyMessage)
    }
    
    // MARK: - Constraints
    
    private func setupConstraints() {
        customLocationTableView.translatesAutoresizingMaskIntoConstraints = false
        customLocationTableView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
        customLocationTableView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
        customLocationTableView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
        customLocationTableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        
        userMessageLabel.translatesAutoresizingMaskIntoConstraints = false
        userMessageLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        userMessageLabel.widthAnchor.constraint(equalToConstant: 288).isActive = true
        userMessageLabel.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 16).isActive = true
    }
}

// MARK: - UITableViewDelegate

extension HikingCustomLocationViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if case .loaded(let customLocationGroups) = props {
            guard
                let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: DEFAULT_HEADER_FOOTER_VIEW_ID) as? DefaultTableViewHeaderFooterView,
                let group = customLocationGroups[safe: section]
                else { return nil }
            headerView.configure(title: ("\(NSLocalizedString("TOUR", comment: "")) \(group.tourId)"), description: nil)
            headerView.contentView.backgroundColor = ColorHelper.bckgTableHeaderDarkBlue
            return headerView
        }
        
        return nil
    }
    
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            tableView.deselectRow(at: indexPath, animated: true)
    
            if case .product(let appStoreId) = globalState.state {
                if case .loaded(let customLocationGroups) = props {
                    guard
                        let group = customLocationGroups[safe: indexPath.section],
                        let cLocation = group.customLocations[safe: indexPath.row],
                        let tourId = Int(group.tourId)
                        else { return }
                    globalState.state = .tour(
                        appStoreId: appStoreId,
                        tourId: tourId,
                        mapSelectionState: .customLocation(id: cLocation.id))
                    let hikingTourContainerVC = HikingTourContainerViewController(
                        exitButtonTitile: NSLocalizedString("PLACESTITLE", comment: ""))
                    self.navigationController?.pushViewController(hikingTourContainerVC, animated: true)
                }
            }
        }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        if case .loaded(let customLocationGroups) = props {
            return customLocationGroups.count
        }
        
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if case .loaded(let customLocationGroups) = props {
            guard let customLocations = customLocationGroups[safe: section] else { return 0 }
            return customLocations.customLocations.count
        }
        
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if case .loaded(let customLocationGroups) = props {
            guard
                let customLocations = customLocationGroups[safe: indexPath.section]?.customLocations,
                let cell = tableView.dequeueReusableCell(withIdentifier: CUSTOM_LOCATION_CELL_ID) as? PlacesTableViewCell,
                let cLocation = customLocations[safe: indexPath.row]
                else { return UITableViewCell() }
            cell.configure(noteText: cLocation.note, date: cLocation.date)
            setSelectedBackgroundViewFor(cell)
            return cell
        }
        
        return UITableViewCell()
    }
    
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        let deleteAction = UITableViewRowAction(style: .destructive, title: NSLocalizedString("DELETE", comment: ""), handler: { (rowAction, indexPath) in
            
           print("Delete action")
        })
        
        return [deleteAction]
    }
    
    private func setSelectedBackgroundViewFor(_ cell: UITableViewCell) {
        let selectedBackgroundView = UIView()
        selectedBackgroundView.backgroundColor = ColorHelper.bckg3DefalutBlue
        cell.selectedBackgroundView = selectedBackgroundView
    }
}

1 Ответ

0 голосов
/ 05 августа 2020

Проблема заключалась в вызове customLocationTableView.reloadData() внутри viewWillLayoutSubviews(). После его перемещения (в моем случае я перемещаю его в props 'didSet) начинают появляться действия редактирования.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...