2

i'm new in swift development, i added data in server tried to refresh tableviewcontroller with refreshcontrol function but value in table view didn't change.

class MainTableViewController: UITableViewController, UINavigationControllerDelegate {

@IBOutlet var sosTableView: UITableView!

var datas = [dataSos]()
override func viewDidLoad() {
    super.viewDidLoad()

    let spinningActivity = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
    spinningActivity.labelText = "Loading"
    spinningActivity.detailsLabelText = "Please wait"
    dispatch_async(dispatch_get_main_queue()) {
        self.loadDataServer()
        spinningActivity.hide(true)
        self.sosTableView.reloadData()
    }


    //loadDataSos()

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

  var refreshControl = UIRefreshControl()
    refreshControl.addTarget(self, action: Selector("refreshData"), forControlEvents: UIControlEvents.ValueChanged)
    self.refreshControl = refreshControl
}

Refresh func

func refreshData(){

    dispatch_async(dispatch_get_main_queue()) {
        self.loadDataServer()

        self.sosTableView.reloadData()
    }

    refreshControl?.endRefreshing()

}

load server func

func loadDataServer(){

    do {

        let data = NSData(contentsOfURL: NSURL(string: "http://xxxx/scripts/xxx.php")!)

        let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)




        //let NumberOfPersons = jsonResult.count

        // **LOOP THROUGH THE JSON ARRAY**
        for anItem in jsonResult as! [Dictionary<String, AnyObject>] {
            let userId = anItem["userId"] as! String
            let userName = anItem["firstName"] as! String
            let userAddress = anItem["address"] as! String
            let userDate = anItem["date"] as! String
            let userLocation = anItem["location"] as! String
            var userEvent = anItem["event"] as? String
            let sosId = anItem["sosId"] as! String
            // do something with personName and personID


             let imageUrl = NSURL(string:"http://xxx")
             let imageData = NSData(contentsOfURL: imageUrl!)


            if userEvent == nil{
                userEvent = "Need Help"

            }else if userEvent! == "1" {
                userEvent! = "Thief"
            }
            else if userEvent! == "2" {
                userEvent! = "Fire"
            }
            else{
                userEvent! = "Healthy Issue"
            }



            //print(personName)
            if imageData == nil{
                let photo1 = UIImage(named: "defaultPhoto")!
                let data1 = dataSos(userId: userId, name: userName, location: userLocation, address: userAddress, event: userEvent!, date: userDate, photo: photo1, sosId: sosId)
                self.datas += [data1]
            }
            else{
            let photo1 = UIImage(data: imageData!)
            //let photo1 = UIImage(named: "defaultPhoto")
                let data1 = dataSos(userId: userId, name: userName, location: userLocation, address: userAddress, event: userEvent!, date: userDate, photo: photo1, sosId: sosId)

            self.datas += [data1]
            }
        }






    } catch let error as NSError {

        print(error)

    }
   // }
}

Update: table view data source

// MARK: - Table view data source

override func numberOfSectionsInTableView(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 datas.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


    let cellIdentifier = "cell"

    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! MainTableViewCell

    // Configure the cell...
    let data0 = datas[indexPath.row]

    cell.nameLabel.text = data0.name
    cell.locationLabel.text = data0.location
    cell.addressTextView.text = data0.address
    cell.eventLabel.text = data0.event
    cell.dateLabel.text = data0.date
    cell.photoLabel.image = data0.photo
    self.roundingUIView(cell.photoLabel, cornerRadiusParam: 35)


    return cell



}
Aldo Lazuardi
  • 1,898
  • 2
  • 19
  • 34
  • Can you also provide your UITableViewDataSource protocol implementation? I mean tableView:cellForRowAtIndexPath: and tableView:numberOfRowsInSection: methods – iyuna Oct 31 '15 at 09:43
  • hey @Iyuna, i just added table view data source that u asked. anyway data only updated if i re-run the apps. thank you – Aldo Lazuardi Oct 31 '15 at 14:23

1 Answers1

0

Ok, I just understood that you're inheriting for UITableViewController, therefor you already have tableView property inherited from it. The table view from this property has already set delegate and dataSource to your controller, but not for your custom sosTableView. You should replace your custom sosTableView with inherited tableView property and then everything gonna work as you're expecting.

iyuna
  • 1,787
  • 20
  • 24
  • yeah thats what i want, what should i do? – Aldo Lazuardi Nov 01 '15 at 10:26
  • sorry i dont get it, so i need to disconnecting reference outlets of 'sosTableView' from storyboard and turn sosTableView.reloadData to tableView.reload data? @Iyuna – Aldo Lazuardi Nov 01 '15 at 11:58
  • You can do one of the following: • delete sosTableView at all and use in all cases tableView property (also connect tableView property to your table view in storyboard if it's not already connected) • set in storyboard your sosTableView's delegate and dataSource to your MainTableViewController – iyuna Nov 01 '15 at 17:39
  • Which variant did you try? – iyuna Nov 02 '15 at 09:23
  • Ok. Could you please share your code with GitHub or whatever so I can check if you have done everything correct? – iyuna Nov 02 '15 at 11:32