Обновите UITableViewCell из класса BaseViewModel - PullRequest
1 голос
/ 31 января 2020

Я пытаюсь обновить TableViewCell с моим stringData из этого метода в моем классе BaseViewModel, stringData восстанавливается из сокета:

func getSocketData(stringData: String) {
        if let jsonData = stringData.data(using: .utf8) {
            let decoder = JSONDecoder()
            do {
                let quotationNew = try decoder.decode(Quotation.self, from: jsonData)
                if self.quotations != nil {
                    for quo in self.quotations! where quo.ticker == quotationNew.ticker {

                        let res = (quotationNew.previousPrice?.toInt() ?? 0) - (quo.previousPrice?.toInt() ?? 0)
                        let model = buildQuotationModel(quotationNew)                        
                        model.dateSocket = quotationNew.date
                        model.sign = self.comparePrice(newPrice: quotationNew.last, variation: Double(res), ticker: quotationNew.ticker!)

                        quo.buyAmount = quotationNew.buyAmount
                        quo.buyPrice = quotationNew.buyPrice
                        quo.volume = quotationNew.volume
                        quo.date = quotationNew.date
                        quo.sellAmount = quotationNew.sellAmount
                        quo.sellPrice = quotationNew.sellPrice
                        quo.minQuotation = quotationNew.minQuotation
                        quo.maxQuotation = quotationNew.maxQuotation
                        quo.previousPrice = quotationNew.previousPrice
                        quo.timestamp = quotationNew.timestamp

                        let index = self.quotations?.index(of: quo)
                        delegate.updateConstraints()
                        self.reloadRow?(model, index!)                        
                        break
                    }
                }
            } catch {
                print(error.localizedDescription)
            }
        } else {
            print(error.debugDescription, " - Error when converting string to Data")
        }
    }

Это мой класс QuotationCell, где я должен обновить мои данные:

func drawCell(_ tableView: UITableView, indexPath: IndexPath) -> UITableViewCell {
        let cell: QuotationCell = tableView.dequeue(for: indexPath)
        cell.delegate = self
        cell.tickerLabel.text = bonusIdentifier
        cell.quotationLabel.text = lastQuotation
        cell.descriptionLabel.text = description
        cell.dateLabel.text = "\("last_update".localized) \(date ?? "")"
        cell.variationContainer.backgroundColor = variationColor
        cell.term = term ?? "-"
        cell.hideLeftIcon = hideLeftIcon
        cell.isInWatchlist = isInWatchlist
        cell.isAbsoluteVariation = isAbsoluteVariation
        cell.variationLabel.text = (isAbsoluteVariation ? absoluteVariationValue : percentageVariationValue) ?? "-"
        cell.variationContainer.isUserInteractionEnabled = absoluteVariation != nil
        cell.onTappedVariation = onTappedVariation
        cell.addBottomBorder(with: .lightBlueGrey, andWidth: 0.5)
        applyEffect(cell)
        return cell
    }

Я пробовал с этим делегатом и всеми этими методами, но у меня не работает

func updateConstraints() {
        self.tableView.updateConstraints()
        self.tableView.updateFocusIfNeeded()
        self.tableView.beginUpdates()
        self.tableView.reloadData()
        self.view.setNeedsDisplay()
    }

Есть предложения?

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