0

First of all, I would like to tell you that I've already tried to search and I've found so many questions with the same problem, but the solutions proposed there didn't work in my case.

I want insert a tableView inside the view of my UIViewController when a specific button is clicked, The data of the UITableView will come from the server.

I have UITableView not UITableViewController

my problem is that the data not being updated unless I scroll

I already found this question UITableViewCell textLabel, does not update until a scroll, or touch happens, while using GCD the solution there is to call setNeedLayout and another guy suggested to use setNeedsDisplay. both didn't solve my problem

This question also raises the same problem and the answer states to call the reloadData, which I'm doing from the first place

This is the delegate and data adapter for my UITableView

class CusinePreferencesTableView: NSObject, UITableViewDelegate, UITableViewDataSource {

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(CellIdentefiers.oneCusinePreferencesCell.rawValue, forIndexPath: indexPath) as! OneCusinePreferencesTableViewCell
        let row = indexPath.row
        print("row = \(row)")
        let oneCusineDataLeft = Preferences2ViewController.cusines![2*row]
        cell.leftButton.titleLabel?.text = oneCusineDataLeft
        if (2*row+1) < Preferences2ViewController.cusines!.count{
            let oneCusineDataRight = Preferences2ViewController.cusines![2*row+1]
            cell.rightButton.titleLabel?.text = oneCusineDataRight
        }else {
            //I should hide the right button
            cell.rightButton.titleLabel?.text = "wow"
        }
        cell.setNeedsDisplay()
        return cell
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let cusines = Preferences2ViewController.cusines {
            if cusines.count % 2 == 0 {
                return cusines.count/2
            }else {
                return (cusines.count+1)/2
            }
        }else {
            return 0
        }
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

}

when someone clicks a button in my UIViewController, I do this:

setConstraintsForTableView(self.cusineTableView)
        loadCusiens()

and finally this is the loadCusines function

func loadCusiens(){
        let url = NSURL(string: ConstantData.getWebserviceFullAddress()+"preferences/cusines")
        let request = NSMutableURLRequest(URL: url!)
        request.HTTPMethod = "POST"
        let session = NSURLSession.sharedSession()
        let task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error ) in

            if let data = data {
                do{
                    // here I handle the response
                    Preferences2ViewController.cusines = results
                    dispatch_async(dispatch_get_main_queue(), {
                        self.cusineTableView.reloadData()
                    })
                } catch{

                }

            }
        })
    task.resume()
}

Eventhough in the morning i asked a similar question, but this is a different one because in the morning i had a not good code in the tableViewCell

Cœur
  • 37,241
  • 25
  • 195
  • 267
sarah
  • 1,201
  • 1
  • 9
  • 29
  • Have you checked that your function is indeed called, and likewise for the completion handler, and the section that updates and reloads the data? Your code obviously lacks the part that converts `data` to `results`. – jcaron Dec 20 '15 at 23:59
  • @jcaron yes sure, its being call and the data is being fetched from the server – sarah Dec 21 '15 at 00:17
  • Where's your conversion from `data` to `results`? – jcaron Dec 21 '15 at 00:32
  • @jcaron well i didn't post that because that is nothing more than just paring the json that comes from the server, it's (at least what I thought) not connected to the problem. i wanted to keep the questoin simple – sarah Dec 21 '15 at 00:39
  • Have you actually checked that `results`contains the right data at that point? Have you checked whether `numberOfSectionsInTableView` and `tableView:numberOfRowsInSection:` are actually called when you trigger the reload, and what values they return? NB: `setNeedsDisplay` is most certainly not needed, and you row count function can be simplified a lot. – jcaron Dec 21 '15 at 01:12
  • @jcaron yes i checked the data a lot, it's really correct, I can give it to you if you want. yes the setNeedsDisplay is not necessary but i added that when i was trying to diagnose the problem. – sarah Dec 21 '15 at 01:18
  • You're only answering half the questions... And when I say "check `results`", I mean actually checking it at the point you save it and then use it. NSLog and breakpoints are your friends. – jcaron Dec 21 '15 at 01:21
  • @jcaron okay I just checked again for you, and here i print the `results` value for you, here you go ["Cusine 0", "Cusine 1", "Cusine 2", "Cusine 3", "Cusine 4", "Cusine 5", "Cusine 6", "Cusine 7", "Cusine 8", "Cusine 9"]`` I did the print call inside the `dispatch ...` – sarah Dec 21 '15 at 01:44
  • @jcaron i need to sleep, if u write something, i'll see tomorrow, thanks for helping – sarah Dec 21 '15 at 02:04
  • Again, many questions that are still unanswered: Have you checked whether `numberOfSectionsInTableView:` and `tableView:numberOfRowsInSection:` are actually called when you trigger the reload, and what values they return? – jcaron Dec 21 '15 at 09:07
  • @jcaron i sware i did, more than 10 times, i put print there, i put breakpoints – sarah Dec 21 '15 at 10:20
  • and the result is? We can't help you if you only provide half of the information... – jcaron Dec 21 '15 at 11:09
  • @sarah I've updated my answer to your previous question (which this question duplicates) - the problem lies in how you set the button title, not the broader process for loading the table view from the data source. – pbasdf Dec 21 '15 at 22:44
  • you can't imagine what the wrong was, it was that i should have used setTitle method, not text = method :( :( – sarah Dec 22 '15 at 01:48

1 Answers1

-1

What is this :

setConstraintsForTableView

Also, try removing the dispatch_async in the network call

ray
  • 102
  • 1
  • 8
  • please forgett about setConstrainsForTable view, that function does nothing exception setting the constratins. i can't remove the dispath_async obviously, – sarah Dec 20 '15 at 23:27
  • @sarah of course you can remove the dispatch. Try it – ray Dec 20 '15 at 23:33
  • @ray of course the dispatch_async can't be removed. The completion handler of a NSURLSession dataTask runs on a background thread, while UIKit calls must be run on the main thread. – jcaron Dec 20 '15 at 23:56
  • @jcaron source to docs ? I've been making UIKit calls in background threads without any problem for a while now – ray Dec 21 '15 at 02:33
  • 1
    @ray either you didn't, or you've not been attentive to the results, it's quite obvious when you do, updates are generally delayed. See https://developer.apple.com/library/ios/technotes/tn2109/_index.html for instance, and numerous SO questions on the topic – jcaron Dec 21 '15 at 09:13