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