如何解决使用其他数据更新后,另一个视图中的表视图消失了
我的表格视图有一个复杂的问题。
我有一个带有自定义单元格的表格视图,其中有另一个表格视图,当我用相同的数组重新加载数据时,它可以正常工作,但是当我使用不同的数据(也只有一个数字)进行更新时,该单元格消失了。
视频:https://streamable.com/cgvg9h
在模拟器中,它在正常的设备中正常工作。
DashboardController.swift
import UIKit
import NVActivityIndicatorView
struct Lotto {
var nome: String
var settori: [Settore]
}
struct Settore {
var nome: String
var data: String
var metriCubi: String
var consiglioIrriguo: String
var aperto: String?
var salta: String?
var incrementa: Int?
var stato: String
var comandoInCorso: String?
}
extension UIView {
func clearConstraints() {
for subview in self.subviews {
subview.clearConstraints()
}
self.removeConstraints(self.constraints)
}
}
class DashboardController: UITableViewController,NVActivityIndicatorViewable,UITabBarControllerDelegate {
@IBOutlet var lottoTableView: UITableView!
@IBOutlet weak var allarmeLabel: UILabel!
@IBOutlet weak var alarmImage: UIImageView!
var avoidFirstReload: Int = 0
var lotti = [Lotto]()
var autoUpdate: Timer = Timer.init()
override func viewDidLoad() {
super.viewDidLoad()
self.tabBarController?.delegate = self
lottoTableView.delegate = self
lottoTableView.dataSource = self
// self.view.viewWithTag(100)!.frame.size.height = 0 // Genera un errore di costraint
if (avoidFirstReload == 0) {
refreshList(self)
}
}
@objc func autoUpdateFunc() {
refreshList(self)
}
override func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = lottoTableView.dequeueReusableCell(withIdentifier: "customLottoCell",for: indexPath) as! LottoCell
let lottiArray = lotti[indexPath.row]
cell.lottoNameLabel?.text = lottiArray.nome
cell.lotto = lottiArray.nome
cell.settori = lottiArray.settori
cell.reloadList()
let openTap = ShowActions(target: self,action: #selector(displayActionSheet(sender:)))
openTap.numberOfTapsrequired = 1
cell.lottoCommandImage.isUserInteractionEnabled = true
cell.lottoCommandImage.addGestureRecognizer(openTap)
openTap.buttons = ["MA","AU","SA","AS","MG","AM"]
openTap.buttons_name = ["commandManual".localized(),"commandAuto".localized(),"commandSkip".localized(),"commandDenySkip".localized(),"commandIncrease".localized(),"commandDenyIncrease".localized()]
openTap.lotto = lottiArray.nome
openTap.settore = nil
let defaults = UserDefaults.standard
let automation = defaults.string(forKey: "automation")
if (automation == "0") {
cell.lottoCommandImage.isHidden = true
}
return cell
}
@IBAction func refreshList(_ sender: Any) {
startAnimating(CGSize(width: 30,height: 30),type: NVActivityIndicatorType.ballpulse,color: UIColor(red: 0/255,green: 133/255,blue: 119/255,alpha: 1),backgroundColor: UIColor.white)
let defaults = UserDefaults.standard
let token = defaults.string(forKey: "token")
let username = defaults.string(forKey: "username")
let param: [String: Any] = [
"Funzione": "ListaLotti","Versione": App.Constants.interfaceVer,"Lingua": Locale.current.languageCode!,"Utente": username!,"Token_Accesso": token!,"Id_dispositivo": UIDevice.current.identifierForvendor!.uuidString,]
App.MakeRequest(json: param) {
resp in
dispatchQueue.main.async {
self.stopAnimating()
if (resp["Errore"] == nil){
let reqStatus = resp["Stato"] as! String
if (reqStatus == "OK"){
self.lotti.removeAll()
let lottiArray = resp["Lotti"] as! [AnyObject]
if (lottiArray.count > 0) {
var settori = [Settore]()
var sNome: String
var sData: String
var sMetriCubi: String
var sCIVolume: String
var sAperto: String?
var sSalta: String?
var sMaggPerc: Int?
var sstato: String
var sComandoInCorso: String?
for lotto in lottiArray {
settori.removeAll()
sNome = ""
sData = ""
sMetriCubi = ""
sCIVolume = ""
sAperto = nil
sSalta = nil
sMaggPerc = nil
sstato = ""
sComandoInCorso = nil
for settore in lotto["Settori"] as! [AnyObject] {
sNome = settore["Settore"] as! String
sData = settore["Data"] as! String
sMetriCubi = settore["Volume_Erogato"] as! String
sCIVolume = settore["Consiglio_Irriguo_Volume"] as! String
sAperto = settore["Aperta"] as? String
sSalta = settore["Salta"] as? String
sMaggPerc = settore["Maggiorazione_Consiglio_Irriguo_Percentuale"] as? Int
sstato = settore["Stato_Settore"] as! String
sComandoInCorso = settore["Comando_Settore_In_Corso"] as? String
// this is a test for changing datas
let rand = Int.random(in: 1..<100)
sMetriCubi = String(rand)
settori.append(Settore(nome: sNome,data: sData,metriCubi: sMetriCubi,consiglioIrriguo: sCIVolume,aperto: sAperto,salta: sSalta,incrementa: sMaggPerc,stato: sstato,comandoInCorso: sComandoInCorso))
}
self.lotti.append(Lotto(nome: lotto["Codice_Lotto"] as! String,settori: settori))
}
}
self.lottoTableView.reloadData()
} else if (reqStatus == "KX") {
...
} else {
...
}
}
}
}
override func tableView(_ tableView: UITableView,heightForRowAt indexPath: IndexPath) -> CGFloat {
return 70+CGFloat(lotti[indexPath.row].settori.count*40)
}
override func tableView(_ tableView: UITableView,estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
override func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
return lotti.count
}
LottoCell.swift
import UIKit
class LottoCell: UITableViewCell,UITableViewDelegate,UITableViewDataSource {
@IBOutlet weak var lottoNameLabel: UILabel!
@IBOutlet weak var lottoCommandImage: UIImageView!
@IBOutlet weak var settoreTableView: UITableView!
var settori = [Settore]()
var lotto = String()
override func awakeFromNib() {
super.awakeFromNib()
settoreTableView.delegate = self
settoreTableView.dataSource = self
}
func reloadList() {
print("reload")
print(settori)
settoreTableView.reloadData()
}
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = settoreTableView.dequeueReusableCell(withIdentifier: "customSettoreCell",for: indexPath) as! SettoreCell
let settore = settori[indexPath.row]
cell.settoreNameLabel?.text = settore.nome
...
return cell
}
func tableView(_ tableView: UITableView,heightForRowAt indexPath: IndexPath) -> CGFloat {
return 40
}
func tableView(_ tableView: UITableView,estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
return settori.count
}
我尝试了stackoverflow中提供的所有解决方案,例如检查高度,检查大小,重新加载视图等...,但是没有用。
非常感谢
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。