Stanley Moon
Stanley Moon

Reputation: 41

Swift: tableView

I have following tableView defined:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("ProductCell", forIndexPath:indexPath)
    print("Pass it to tableview \(courseName2) ")
     print("Pass it to tableview \(codeName2) ")
    var count = 0


    getTime( (self.courseName2!), courseCode: (self.codeName2!), completion:{(success:[ClassSchedule]) -> Void in
            count = classes[0].total!
            print("TOTAL CKASSESSSs \(count) ")
            dispatch_async(dispatch_get_main_queue(),{
                self.tableView.reloadData()
                for var i = 0; i < count; ++i
                {
                    //print("FOR LOOP COUNT: \(i)")
                    cell.textLabel?.text = "\(classes[i].section!)"
                    // Start Time: \(classes[i].start!)End Time: \(classes[i].end!)
                    print("FOR LOOP COUNT: \(i) Section\(classes[i].section!)")
                }

            });

    })
    return cell
}

The print statement in the for loop function provides correct information from the 'classes' array, but when I simulate the code, only the last element fills up every rows.

Screenshot of simulator

Instead of 'TST 101', I want it to display 'LEC 001', 'LEC 002', 'LEC 003', 'LEC 004', 'TST 101' in order, which are contained in 'classes' array.

getTime function, getting data through JSON call:

 // Read the JSON
    do {
        let data: NSData? = NSData(contentsOfURL: url)
        if let jsonResult: NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary
        {
            //////ARRAY///////
            let dataArray = jsonResult["data"] as! NSArray
            //////
            let arrayLength = dataArray.count
            var count = 0
            //classes.append(ClassSchedule(course: item["subject"] as! String, code: item["catalog_number"] as! String, section: item["section"] as! String) )

            print("TOTAL OF CLASSES: \(arrayLength)");

            for item in dataArray
            {

                classes.append(ClassSchedule(course: "TEMP", code: "TEMP", section: "TEMP", days:"TEMP", start:"TEMP", end:"TEMP", building:"TEMP", room:"TEMP", total: arrayLength) )

                classes[count].course = (item["subject"] as! String)
                classes[count].code = (item["catalog_number"] as! String)
                classes[count].section = (item["section"] as! String)
                print("Subject: \(classes[count].course!) \(classes[count].code!)");
                print("Section: \(classes[count].section!)");
              // print("Section: \(section_numb)");

                let subjectArray = item["classes"] as! NSArray

                for item2 in subjectArray{

                    let dateDictionary = item2["date"] as! NSDictionary
                    //let startTime = dateDictionary["start_time"]!
                    //self.performSelectorOnMainThread("updateIPLabel:", withObject: startTime, waitUntilDone: false)
                    //let endTime = dateDictionary["end_time"]!
                    classes[count].days = (dateDictionary["weekdays"] as! String)
                    classes[count].start = (dateDictionary["start_time"] as! String)
                    classes[count].end = (dateDictionary["end_time"] as! String)
                    string1 = classes[count].start!
                    print("Weekdays: \(classes[count].days!)");
                    print("START TIME: \(classes[count].start!)");
                    print("End Time: \( classes[count].end!)");


                    let locationDictionary = item2["location"] as! NSDictionary
                    classes[count].building = (locationDictionary["building"] as? String)
                    classes[count].room = (locationDictionary["room"] as? String)
                    print("Building: \(classes[count].building) \(classes[count].room)");


                    count += 1
                    print(count)
                    print("")

                }

            }
            //let subject = dataArray["subject"]
        }

    } catch {
        print("bad things happened")
    }

    //print("ASDIAWDWD: \(classes[0].section)")
   // print("ASDIAWDWASDASDD: \(classes[1].section)")
    completion(classes: classes)
    }).resume()

Upvotes: 0

Views: 136

Answers (1)

Aaron Brager
Aaron Brager

Reputation: 66302

It's hard to tell what you're trying to do exactly, but you shouldn't be using any asynchronous methods within your table view datasource methods. These methods should only represent data you already have. Seems like you could just replace that entire getTime block with just cell.textLabel?.text = "\(classes[indexPath.row].section!)".

Upvotes: 1

Related Questions