Refresh function does not work as expected Swift 3 - ios

So, I have this app which loads rss feeds and I have implemented refresh function. Upload refreshing, it does reloads and adds the new post if it's posted but the previous data stays there and gets added on to the stack. Check the code and images. I am attaching below.
//
// TableViewController.swift
// WebView
//
import UIKit
class TableViewController: UITableViewController,XMLParserDelegate{
var currentElement:String = ""
var postTitle:String = ""
var webLink:String = ""
var feeds:[Model] = []
var reload: UIRefreshControl! = UIRefreshControl()
override func viewDidLoad() {
super.viewDidLoad()
//let urlToSend: NSURL = NSURL(string: "https://sensodyneforsensitiveteeth.wordpress.com/feed")!
let urlToSend: NSURL = NSURL(string: "http://froshgeek.com/feed")!
let parser = XMLParser(contentsOf: urlToSend as URL)!
parser.delegate = self
parser.parse()
//reload
reload!.attributedTitle = NSAttributedString(string: "Pull to reload the page")
reload!.addTarget(self, action: #selector(TableViewController.reloadFunc), for: UIControlEvents.valueChanged)
tableView.addSubview(reload)
reload.endRefreshing()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
currentElement=elementName;
if(elementName=="item")
{
postTitle = String()
webLink = String()
}
}
func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
if(elementName=="item"){
let feed: Model = Model()
feed.postTitle = postTitle
feed.webLink = webLink
feeds.append(feed)
}
}
func parser(_ parser: XMLParser, foundCharacters string: String) {
let data = string.replacingOccurrences(of: "^\\s*", with: "", options: .regularExpression)
if (!data.isEmpty){
if currentElement == "title" {
postTitle += data
}
else if currentElement == "link" {
webLink += data
}
}
}
func parser(_ parser: XMLParser, parseErrorOccurred parseError: Error) {
print("Bad XML File, Please validate")
}
func reloadFunc(_ sender: AnyObject){
viewDidLoad()
self.tableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return feeds.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "webFeeds", for: indexPath)
let feeds: Model = self.feeds[indexPath.row]
cell.textLabel?.text = feeds.postTitle
return cell
}
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "webParse" {
let feed: Model = feeds[tableView.indexPathForSelectedRow!.row]
let pageView = segue.destination as! ViewController
pageView.webLink = feed.webLink
}
}
func parserDidEndDocument(_ parser: XMLParser){
self.tableView.reloadData();
self.refreshControl?.endRefreshing();
}
}
Model.swift
//
// Model.swift
// WebView
//
import Foundation
class Model {
var postTitle:String = ""
var webLink:String = ""
var currentElement:String = ""
}
ViewController.swift
//
// ViewController.swift
// WebView
//
import UIKit
class ViewController: UIViewController, UIWebViewDelegate {
#IBOutlet weak var showPage: UIWebView!
var webLink: String = String()
override func viewDidLoad() {
super.viewDidLoad()
let link : NSURL = NSURL(string: webLink)!
let req : NSURLRequest = NSURLRequest(url: link as URL)
showPage.loadRequest (req as URLRequest)
showPage.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Screenshot upon reloading

The reason it is adding object at last is because you need to empty your array before refreshing data. Also never call viewDidLoad explicitly what you need to do is make one method that make this API request and call that method from viewDidLoad and your refresh method.
override func viewDidLoad() {
super.viewDidLoad()
//call function to get xml data
self.getData()
//reload
reload!.attributedTitle = NSAttributedString(string: "Pull to reload the page")
reload!.addTarget(self, action: #selector(TableViewController.reloadFunc), for: UIControlEvents.valueChanged)
tableView.addSubview(reload)
reload.endRefreshing()
}
func getData() {
//Make array empty first
feeds = []
let urlToSend: NSURL = NSURL(string: "http://froshgeek.com/feed")!
let parser = XMLParser(contentsOf: urlToSend as URL)!
parser.delegate = self
parser.parse()
}
Now call this getData function in your refresh action method reloadFunc
func reloadFunc(_ sender: AnyObject){
self.getData()
self.tableView.reloadData()
}

Related

How to print data in empty array

I'm trying to print the chat array that is declared as a empty global variable in a table. The data that I'm trying to print is received using web sockets. I'm assigning the data in the messageReceived function, and I know that the data is getting to the program because I'm printing in a label, but the moment that I'm trying to print it in the table is simple not working. All of this is in the ViewController.swift:
import UIKit
import Starscream
var messagetext: String = ""
var tabletext: String = ""
var chat = [String] ()
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {
var socket = WebSocket(url: URL(string: "ws://localhost:1337/")!, protocols: ["chat"])
#IBOutlet weak var chatMessage: UILabel!
#IBOutlet weak var textField: UITextField!
#IBOutlet weak var tableView: UITableView!
#IBAction func buttonClick(_ sender: Any) {
messagetext = textField.text!
sendMessage(messagetext)
}
override func viewDidLoad() {
super.viewDidLoad()
self.textField.delegate = self
socket.delegate = self
socket.connect()
navigationItem.hidesBackButton = true
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
func textFieldDidEndEditing(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return (true)
}
deinit{
socket.disconnect(forceTimeout: 0)
socket.delegate = nil
}
}
// MARK: - FilePrivate
fileprivate extension ViewController {
func sendMessage(_ messager: String) {
socket.write(string: messager)
}
func messageReceived(_ message: String) {
chatMessage.text = message
chat.append(message)
}
}
// MARK: - WebSocketDelegate
extension ViewController : WebSocketDelegate {
public func websocketDidConnect(_ socket: Starscream.WebSocket) {
}
public func websocketDidDisconnect(_ socket: Starscream.WebSocket, error: NSError?) {
performSegue(withIdentifier: "websocketDisconnected", sender: self)
}
public func websocketDidReceiveMessage(_ socket: Starscream.WebSocket, text: String) {
// 1
guard let data = text.data(using: .utf16),
let jsonData = try? JSONSerialization.jsonObject(with: data),
let jsonDict = jsonData as? [String: Any],
let messageType = jsonDict["type"] as? String else {
return
}
// 2
if messageType == "message",
let messageData = jsonDict["data"] as? [String: Any],
let messageText = messageData["text"] as? String {
messageReceived(messageText)
}
}
public func websocketDidReceiveData(_ socket: Starscream.WebSocket, data: Data) {
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return(chat.count)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
cell.textLabel?.text = chat[indexPath.row] as! String
return(cell)
}
}
Assuming that you are sure about there is data to be received by your view controller, The issue would be: the tableview data source methods are called before receiving any data, which means chat data source array is still empty, thus there is no data to display.
The solution for your case is to make sure to reload the tableview after receiving data (updating the value of chat data source array), which means in your case after appending a message to chat in messageReceived method by calling reloadData() UITableView instance method:
func messageReceived(_ message: String) {
chatMessage.text = message
chat.append(message)
// here we go:
tableView.reloadData()
}
In your message received handler, issue a tableview.reloadData()
Cheers!
You need to tell the tableview that there is new data. You also need to allow for the fact that the network operation probably occurred on a background queue and UI updates must be on the main queue:
func messageReceived(_ message: String) {
DispatchQueue.main.async {
let newRow = IndexPath(row: chat.count, section:0)
chatMessage.text = message
chat.append(message)
tableView.insertRows(at:[newRow],with: .automatic)
}
}

GoogleMaps / GooglePlaces leaks memory on iPhone - swift

I'm currently encountering a problem within my app. I have a view on which I can search and select a place with the GoogleMaps / GooglePlaces SDK. I'm also using Google Autocomplete for completing the search string.
The problem: When I visit the first view, everything is still fine, but after I have entered a search string and press search, my memory usage increases for about 10-20 MB. When I unwind the view and save it, the memory won't be released and is still in my total memory. Therefore if I save a couple of places I'll get an overflow and the App will crash.
Do you guys know where my problem is in the code? I'm already searching for hours...
I looked for Database request which sets up listeners but in fact nothing happens regarding this in these classes. Maybe it has something to do with Googlemaps, that it keeps up the connection?
I attached 2 screenshots from my app and the 2 classes for these are the following:
import UIKit
import GoogleMaps
class AddNewPlaceViewController: UIViewController, UISearchBarDelegate, LocateOnTheMap {
//Outlets
#IBOutlet weak var googleMapsContainer: UIView!
//Variables
var googleMapsView: GMSMapView!
var searchResultController: SearchResultsController!
var resultsArray = [String]()
//Result
var locationAsCoords = [String:Double]()
var locationAdress = String()
//Error
var alerts = Alerts()
var alertActions = AlertActions()
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(true)
self.googleMapsView = GMSMapView(frame: self.googleMapsContainer.frame)
self.view.addSubview(self.googleMapsView)
searchResultController = SearchResultsController(view: self)
searchResultController.delegate = self
showSearchBar()
}
#IBAction func saveButtonTapped(sender: AnyObject) {
if self.locationAdress.isEmpty {
// Error message if no address has been searched
NSLog("Event address ist empty.")
let alert = alerts.alertNoLocationAddress
let alertCancel = alertActions.alertActionCancel
if(alert.actions.count == 0){
alert.addAction(alertCancel)
}
self.presentViewController(alert, animated: true, completion: nil)
} else {
// exits with the saveAndUnwind segue
self.performSegueWithIdentifier("saveAndUnwind", sender: self)
}
}
/**
action for search location by address
- parameter sender: button search location
*/
#IBAction func searchWithAddress(sender: AnyObject) {
showSearchBar()
}
/**
Locate map with latitude and longitude after search location on UISearchBar
- parameter lon: longitude location
- parameter lat: latitude location
- parameter title: title of address location
*/
func locateWithLongitude(lon: Double, andLatitude lat: Double, andTitle title: String) {
dispatch_async(dispatch_get_main_queue()) { () -> Void in
self.googleMapsView.clear()
let position = CLLocationCoordinate2DMake(lat, lon)
let marker = GMSMarker(position: position)
let camera = GMSCameraPosition.cameraWithLatitude(lat, longitude: lon, zoom: 15)
self.googleMapsView.camera = camera
marker.title = "\(title)"
marker.map = self.googleMapsView
}
}
func showSearchBar(){
let searchController = UISearchController(searchResultsController: searchResultController)
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.delegate = self
self.presentViewController(searchController, animated: true, completion: nil)
}
/**
Searchbar when text change
- parameter searchBar: searchbar UI
- parameter searchText: searchtext description
We can use filters here in the autocompleteQuery
*/
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
let placeClient = GMSPlacesClient()
placeClient.autocompleteQuery(searchText, bounds: nil, filter: nil) { (results, error: NSError?) -> Void in
self.resultsArray.removeAll()
if results == nil {
return
}
for result in results! {
if let result = result as? GMSAutocompletePrediction {
self.resultsArray.append(result.attributedFullText.string)
}
}
self.searchResultController.reloadDataWithArray(self.resultsArray)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "saveAndUnwind"){
//Pushing the data to the other view controller
let destViewControllerCreateEventTable = segue.destinationViewController as! CreateEventTableViewController
destViewControllerCreateEventTable.locationAsCoords["latitude"] = self.locationAsCoords["latitude"]
destViewControllerCreateEventTable.locationAsCoords["longitude"] = self.locationAsCoords["longitude"]
destViewControllerCreateEventTable.locationAdress = self.locationAdress
destViewControllerCreateEventTable.locationLabel.text = self.locationAdress
destViewControllerCreateEventTable.locationLabel.textColor = UIColor.darkGrayColor()
}
}
}
//
import UIKit
protocol LocateOnTheMap{
func locateWithLongitude(lon:Double, andLatitude lat:Double, andTitle title: String)
}
class SearchResultsController: UITableViewController {
var searchResults: [String]!
var delegate: LocateOnTheMap!
var addNewPlaceReference = AddNewPlaceViewController()
init(view: AddNewPlaceViewController ){
super.init(style: UITableViewStyle.Plain)
addNewPlaceReference = view
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
self.searchResults = Array()
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cellIdentifier")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.searchResults.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cellIdentifier", forIndexPath: indexPath)
cell.textLabel?.text = self.searchResults[indexPath.row]
return cell
}
override func tableView(tableView: UITableView,
didSelectRowAtIndexPath indexPath: NSIndexPath){
// 1
self.dismissViewControllerAnimated(true, completion: nil)
// 2
let correctedAddress:String! = self.searchResults[indexPath.row].stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.symbolCharacterSet())
let url = NSURL(string: "https://maps.googleapis.com/maps/api/geocode/json?address=\(correctedAddress)&sensor=false")
let task = NSURLSession.sharedSession().dataTaskWithURL(url!) { (data, response, error) -> Void in
// 3
do {
if data != nil{
let dic = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableLeaves) as! NSDictionary
let lat = dic["results"]?.valueForKey("geometry")?.valueForKey("location")?.valueForKey("lat")?.objectAtIndex(0) as! Double
let lon = dic["results"]?.valueForKey("geometry")?.valueForKey("location")?.valueForKey("lng")?.objectAtIndex(0) as! Double
// 4
self.delegate.locateWithLongitude(lon, andLatitude: lat, andTitle: self.searchResults[indexPath.row])
self.addNewPlaceReference.locationAdress = self.searchResults[indexPath.row]
self.addNewPlaceReference.locationAsCoords["latitude"] = lat
self.addNewPlaceReference.locationAsCoords["longitude"] = lon
}
}catch {
print("Error")
}
}
// 5
task.resume()
}
func reloadDataWithArray(array:[String]){
self.searchResults = array
self.tableView.reloadData()
}
}

Can't get an array from another view controller Swift

I'm making an Orientation (don't know if it's the right name in english) app. You can scan a Qr code and then you can send the results to your teacher. I'm using Multipeer connection to send the results from the students phone and the teachers. I have one UITableViewController and I want to make an array (with a struct, 2 values) available for another view controller -> UIViewController. And there's the problem. When I try to "send" it to the other viewControler it doesn't reach to the end ore something. I have tried everything (controlsGetShared is one try) I can, but still it doesn't work... :(
The Struct looks like this
struct Control {
let name: String
let code: String
}
The UITableViewController looks like
import UIKit
class ControlViewController: UITableViewController {
var controlsGet = [Control] ()
var controlsGetShare = [Control] ()
var def = NSUserDefaults.standardUserDefaults()
var valueName: String = ""
var valueCode: String = ""
#IBAction func done(segue: UIStoryboardSegue) {
var scanVc = segue.sourceViewController as ScanViewController
valueName = scanVc.name
valueCode = scanVc.code
println("sendToSendFile \(valueName) \(valueCode)")
controlsGet.append(Control(name: valueName, code: valueCode))
//controlsGetShare.append(Control(name: valueName, code: valueCode))
println("\(self.controlsGet.count) shareArray \(self.controlsGetShare.count)")
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
controlsGetShare = controlsGet
println("preparaForSegueCount \(controlsGetShare.count)")
}
override func viewDidLoad() {
println("viewDidLoad")
tableView.reloadData()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.controlsGet.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
//for index in controlsGet {
var control : Control
control = controlsGet[indexPath.row]
cell.textLabel.text = control.name
cell.detailTextLabel?.text = control.code
//}
return cell
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
controlsGet.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
} else if editingStyle == .Insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
}
Ant the send UIViewControler
import UIKit
import MultipeerConnectivity
class SendFileViewController: UIViewController, MCBrowserViewControllerDelegate,
MCSessionDelegate {
let serviceType = "LCOC-Chat"
var controlsGetShare = [Control] ()
#IBAction func done(segue: UIStoryboardSegue) {
var controlVc = segue.sourceViewController as ControlViewController
println(controlVc.controlsGetShare.count)
controlsGetShare = controlVc.controlsGetShare
println(controlsGetShare.count)
}
var browser : MCBrowserViewController!
var assistant : MCAdvertiserAssistant!
var session : MCSession!
var peerID: MCPeerID!
#IBOutlet var chatView: UITextView!
#IBOutlet var messageField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
self.peerID = MCPeerID(displayName: UIDevice.currentDevice().name)
self.session = MCSession(peer: peerID)
self.session.delegate = self
// create the browser viewcontroller with a unique service name
self.browser = MCBrowserViewController(serviceType:serviceType,
session:self.session)
self.browser.delegate = self;
self.assistant = MCAdvertiserAssistant(serviceType:serviceType,
discoveryInfo:nil, session:self.session)
// tell the assistant to start advertising our fabulous chat
self.assistant.start()
}
#IBAction func sendChat(sender: UIButton) {
println("sendButton")
// Bundle up the text in the message field, and send it off to all
// connected peers
var error : NSError?
var message = ""
/*
var msg = message.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
self.session.sendData(msg, toPeers: self.session.connectedPeers, withMode: MCSessionSendDataMode.Unreliable, error: &error)
*/
println(self.controlsGetShare.count)/*
for index in controlsGetShare {
message = index.name
var msg = message.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
self.session.sendData(msg, toPeers: self.session.connectedPeers, withMode: MCSessionSendDataMode.Unreliable, error: &error)
if error != nil {
print("Error sending nameData: \(error?.localizedDescription)")
}
println("didSendDataA")
message = index.code
msg = message.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
self.session.sendData(msg, toPeers: self.session.connectedPeers, withMode: MCSessionSendDataMode.Unreliable, error: &error)
if error != nil {
print("Error sending codeData: \(error?.localizedDescription)")
}
println("didSendDataB")
}*/
}
#IBAction func showBrowser(sender: UIButton) {
// Show the browser view controller
self.presentViewController(self.browser, animated: true, completion: nil)
}
func browserViewControllerDidFinish(
browserViewController: MCBrowserViewController!) {
// Called when the browser view controller is dismissed (ie the Done
// button was tapped)
self.dismissViewControllerAnimated(true, completion: nil)
}
func browserViewControllerWasCancelled(
browserViewController: MCBrowserViewController!) {
// Called when the browser view controller is cancelled
self.dismissViewControllerAnimated(true, completion: nil)
}
func session(session: MCSession!, didReceiveData data: NSData!,
fromPeer peerID: MCPeerID!) {
// Called when a peer sends an NSData to us
// This needs to run on the main queue
dispatch_async(dispatch_get_main_queue()) {
var msg = NSString(data: data, encoding: NSUTF8StringEncoding)
//self.updateChat(msg, fromPeer: peerID)
}
}
// The following methods do nothing, but the MCSessionDelegate protocol
// requires that we implement them.
func session(session: MCSession!,
didStartReceivingResourceWithName resourceName: String!,
fromPeer peerID: MCPeerID!, withProgress progress: NSProgress!) {
// Called when a peer starts sending a file to us
}
func session(session: MCSession!,
didFinishReceivingResourceWithName resourceName: String!,
fromPeer peerID: MCPeerID!,
atURL localURL: NSURL!, withError error: NSError!) {
// Called when a file has finished transferring from another peer
}
func session(session: MCSession!, didReceiveStream stream: NSInputStream!,
withName streamName: String!, fromPeer peerID: MCPeerID!) {
// Called when a peer establishes a stream with us
}
func session(session: MCSession!, peer peerID: MCPeerID!,
didChangeState state: MCSessionState) {
// Called when a connected peer changes state (for example, goes offline)
}
}
Much of the code are from source codes I found on the internet, so that's why all the comments.Thanks
//Anton

Pull to Refresh in Swift not Reloading UITableView

I've got JSON filling my UITableView successfully, but the JSON is often updated so I need the ability to refresh. I followed THIS TUTORIAL to implement a pull to refresh control. Visually, it seems like it all works correctly, but when I call tableView.reloadData() the table doesn't reload. However, if I leave the ViewController and return, the table is updated. Why would tableView.reloadData() work in viewDidAppear and viewWillAppear but not in my custom refresh() function?
MainVC.swift file
class MainVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet var tableView: UITableView!
var dataArray: NSArray = NSArray()
#IBOutlet var Controller: UISegmentedControl!
var refreshControl:UIRefreshControl!
func refresh(sender:AnyObject)
{
refreshBegin("Refresh",
refreshEnd: {(x:Int) -> () in
self.tableView .reloadData()
println("Table Reloaded")
self.refreshControl.endRefreshing()
})
}
func refreshBegin(newtext:String, refreshEnd:(Int) -> ()) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
println("refreshing")
sleep(2)
dispatch_async(dispatch_get_main_queue()) {
refreshEnd(0)
}
}
}
override func viewWillAppear(animated: Bool) {
self.tableView .reloadData()
}
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.titleView = UIImageView(image: UIImage(named: "logojpg.jpg"))
startConnectionAt("http://www.domain.com/json.php")
refreshControl = UIRefreshControl()
refreshControl.backgroundColor = UIColor.orangeColor()
refreshControl.tintColor = UIColor.whiteColor()
refreshControl.attributedTitle = NSAttributedString(string: "Pull to Refresh")
refreshControl.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)
tableView.addSubview(refreshControl)
}
//MARK: JSON Loading
var data: NSMutableData = NSMutableData()
func startConnectionAt(urlPath: String){
var url: NSURL = NSURL(string: urlPath)
var request: NSURLRequest = NSURLRequest(URL: url)
var connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: false)
connection.start()
}
func connection(connection: NSURLConnection!, didFailWithError error: NSError!) {
println("Connection failed.\(error.localizedDescription)")
}
func connection(connection: NSURLConnection, didRecieveResponse response: NSURLResponse) {
println("Recieved response")
}
func connection(didReceiveResponse: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
self.data = NSMutableData()
}
func connection(connection: NSURLConnection!, didReceiveData data: NSData!) {
self.data.appendData(data)
}
func connectionDidFinishLoading(connection: NSURLConnection!) {
var dataAsString: NSString = NSString(data: self.data, encoding: NSUTF8StringEncoding)
var err: NSError
var json: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
var results: NSArray = json["needs"] as NSArray
self.dataArray = results
tableView.reloadData()
println("success")
}
//End loading of JSON
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return self.dataArray.count;
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
var cell:CustomCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as CustomCell
var rowData: NSDictionary = dataArray[indexPath.row] as NSDictionary
var firstName=rowData["needFirstname"] as String
var descrip=rowData["needDescription"] as String
var poster=rowData["needPoster"] as String
var city=rowData["needCity"] as String
var state=rowData["needState"] as String
var country=rowData["needCountry"] as String
cell.needFirstName.text = firstName
cell.needDescription.text = descrip
cell.needDescription.numberOfLines = 0
cell.needPoster.text = poster
cell.needCity.text = city
cell.needState.text = state
cell.needCountry.text = country
return cell
}
#IBAction func Change(sender: AnyObject) {
if Controller.selectedSegmentIndex == 0 {
startConnectionAt("http://www.domain.com/localJSON.php")
}
else if Controller.selectedSegmentIndex == 1 {
startConnectionAt("http://www.domain.com/intlJSON.php")
}
self.tableView .reloadData()
}
}
Your last comment is right-on in my view.
During your pull to refresh function, you call tableView.reloadData(), however, reloadData() does not inherently do any repopulating the elements in the data source (in your case, dataArray). It simply reloads all the data that's currently in the table view's data source at the time it is called.
So my recommendation would be to construct your refresh function such that the following happens:
Initiate a request to your web service.
When the response comes back (ie, connectionDidFinishLoading is executed), parse the JSON results and assign that result to the dataArray instance. You seem to be doing this already in connectionDidFinishLoading, so it's just a matter of sending the request to your web service, I'd think.
Call tableView.reloadData() to display any new elements that have been added since the last time the tableView's data was displayed. Again, you're doing this already in connectionDidFinishLoading, so #1 is the primary thing that I think needs to happen.
Referring to https://stackoverflow.com/a/25957339
Not sure but maybe the connection is run on a different thread, if so you need to run the table update on the main UI thread
// using Swift's trailing closure syntax:
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}

Xml parser function never gets called

I'm currently working with swift and trying to make and RSS reader iOS iPhone app. I'm currently having trouble with calling the parser function. I'm really trying to replicate what I developed in Objective-C (which works well) with Swift.
This is my tableView.swift class
import UIKit
class FeedTableViewController: UITableViewController, NSXMLParserDelegate {
var parser: NSXMLParser = NSXMLParser()
var feeds: NSMutableArray = []
var fItem = Dictionary<String, Float>()
var fTitle: String = String()
var element: String = String()
override func viewDidLoad() {
super.viewDidLoad()
var url: NSURL = NSURL.URLWithString("http://feeds.feedburner.com/TouchCodeMagazine")
parser = NSXMLParser(contentsOfURL: url)
parser.delegate = self
parser.shouldResolveExternalEntities = false
parser.parse()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// #pragma mark - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
return 1
}
override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
return feeds.count
}
override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {
let CellId: NSString = "Cell"
var cell: UITableViewCell = tableView?.dequeueReusableCellWithIdentifier(CellId) as UITableViewCell
if let ip = indexPath {
cell.textLabel.text = "hello\(ip.row)"
}
return cell
}
// This function is never called.
func parser(parser: NSXMLParser!, didStartElement elementName: String!, nameSpaceURI namespaceURI: String!, qualifiedName: String!, attributes attributeDict: Dictionary<String, Float>) {
element = elementName
println(element) // This line is never executed.
}
}
I have set the NSXMLParserDelegate but still it never gets called.
Any idea on what I can do to get this working? This is all the code I have so far. Maybe I need to import e certain framework?
Try:
func parser(parser: NSXMLParser!, didStartElement elementName: String!, namespaceURI: String!, qualifiedName: String!, attributes: Dictionary<String, Float>) {
println(elementName) // This line is never executed.
}
Correction: Actually, it's not the last parameter, it's the spelling of namespaceURI vs. nameSpaceURI in your original.
Copied and pasted again out of working playground.

Resources