Answer the question
In order to leave comments, you need to log in
tableView.reloadData() method doesn't work. What could be the problem?
Good evening! Already broke my head, and I just can not find a solution.
And so, there is an application that parses XML and displays data in a UITableView. To switch sources, a menu was implemented using UIStackView, the actions for the buttons are as follows:
switch city {
case .topNews:
listOfNewsVM.removeAll()
tableView.reloadData()
feedUrl = "https://tass.ru/rss/v2.xml"
fetchXMLData()
}
func fetchXMLData() {
XMLParserFactory.fetchData(url: feedUrl) { (listOfXMLVM, error) in
if error == nil {
self.listOfNewsVM = listOfXMLVM!
self.tableView.reloadData()
}
else {
print(error?.localizedDescription ?? "Error")
}
}
}
class XMLParserFactory {
static func fetchData(url: String, withCallback completionHandler: @escaping (_ listOfModels: [XMLParserVM]?, _ error: Error?) -> Void) {
XMLParserClient.sharedInstance().fetchXMLData(for: url) { (newsModelList, error) in
print("Fetch data Factory ", newsModelList?.count ?? "0")
if error == nil {
var listOfNewsVM = [XMLParserVM]()
newsModelList?.forEach { model in
let parserVM = XMLParserVM.init(model)
listOfNewsVM.append(parserVM)
}
// for item in listOfNewsVM {
// print("\(item.title) - \(item.link)")
// }
completionHandler(listOfNewsVM, nil)
}
else {
completionHandler(nil, error)
}
}
}
}
class XMLParserVM {
var model:NewsModel?
init(_ model:NewsModel) {
self.model = model
}
var title: String {
if let _ = model {
return (model?.title)!
}
return ""
}
var link: String {
if let _ = model {
return (model?.link)!
}
return ""
}
var desc: String {
if let _ = model {
return (model?.desc)!
}
return ""
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question