A
A
Artem2018-04-07 21:53:50
iOS
Artem, 2018-04-07 21:53:50

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()
        }

1. We erase all the data in the table.
2. We reboot it.
3. Set the variable to a value
4. Execute the XML Parsing function.
The data is entered, but the old ones are not deleted. It turns out that to see the new data, you need to scroll through the old ones. How can I make it so that when the function is restarted, the old data is erased and only the new ones remain?
Function that is in MainViewController:
func fetchXMLData() {
        
        XMLParserFactory.fetchData(url: feedUrl) { (listOfXMLVM, error) in
    
            if error == nil {
               
                self.listOfNewsVM = listOfXMLVM!
                self.tableView.reloadData()
               
            }
            else {
                print(error?.localizedDescription ?? "Error")
            }
        }
    }

And here is the main file where the XML processing takes place:
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 ""
    }

}

I will be very grateful for your help. The most important thing is to clear the old data in the table and display only the new ones.
PS I also noticed such a moment when I receive new data when I click on the button, the counter "print("Fetch data Factory ", newsModelList?.count ?? "0")" displays the total number of data, i.e. old and new . It turns out that the old data is not erased.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question