Samah Ahmed Samah Ahmed - 4 months ago 14
iOS Question

Tableview doesn't display data in swift 2

I'm working on the tableView in swift 2.2 in xcode 7.3.1 and I'm sure from my code because it's not the first time for me to deal with tableView , I'm pulling data correctly from server and stored it in array but I notice the two function that is related to table view is not called so the table view appear empty for me ! I added cell and linked tableview with view also from layout.

I don't know where is the problem!

class studentTeacherList: UIViewController , UITableViewDataSource,UITableViewDelegate {

@IBOutlet weak var studentParentTable: UITableView!


@IBOutlet weak var loadIndicator: UIActivityIndicatorView!




var username:String!
var fromSender: String?
var toRec: String?

var student_id = [Int]()
var parent_id = [String]()
var student_names = [String]()
var parent_name = [String]()



//Sent Data
var s_id:Int = 0
var s_name = ""
var p_id = ""
var p_name = ""


override func viewDidLoad() {
super.viewDidLoad()
studentParentTable.delegate = self
studentParentTable.dataSource = self


let prefs:NSUserDefaults = NSUserDefaults.standardUserDefaults()
username = prefs.objectForKey("user")as! String
fromSender = prefs.objectForKey("Sender")as! String
toRec = prefs.objectForKey("Receiver")as! String


self.loadIndicator.startAnimating()

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> Void in

self.loadList()
//self.studentParentTable.reloadData()


dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.loadIndicator.stopAnimating()
})
});


studentParentTable.reloadData()

}




func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return student_names.count
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//This method to define each cell at Table View
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")


cell.textLabel?.text = student_names[indexPath.row]
cell.detailTextLabel?.text = parent_name[indexPath.row]

return cell
}




override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBAction func backButton(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil )

}



func loadList()
{
var normallink = "mylinkhere"
normallink = normallink + "?teacherid=" + self.username



var studentParentURL:NSURL = NSURL (string: normallink)!
let data = NSData(contentsOfURL: studentParentURL)!



do {
let json = try NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments)

if let alldata = json["data"] as? [[String: AnyObject]] {
for onedata in alldata {
if let stu_id = onedata["id"] as? Int {
student_id.append(stu_id)
}
if let stu_name = onedata["studentName"] as? String {
student_names.append(stu_name)
}
if let par_id = onedata["parentId"] as? String {
parent_id.append(par_id)
}
if let par_name = onedata["parentName"] as? String {
parent_name.append(par_name)
}



}
}
} catch {
print("Error Serializing JSON: \(error)")
}




print(student_names.count)

}


}

Answer

It looks like you aren't reloading after this call:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> Void in

       self.loadList()
       //self.studentParentTable.reloadData()


        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            self.loadIndicator.stopAnimating()
        })
    });

So you should add studentParentTable.reloadData() after self.loadIndicator.stopAnimating().

Comments