Tableview不会在swift 2中显示数据

时间:2016-07-25 20:58:33

标签: ios swift xcode swift2 xcode7

我正在使用xcode 7.3.1中的swift 2.2中的tableView工作,我确信从我的代码开始,因为这不是我第一次处理tableView,我&#39 ; m正确地从服务器提取数据并将其存储在数组中但我注意到没有调用与表视图相关的两个函数,所以表视图对我来说显示为空!我从布局中添加了单元格和链接的tableview以及视图。

我不知道问题出在哪里!

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)

}


    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {


    s_id = student_id[indexPath.row]
    s_name = student_names[indexPath.row]
    p_id = parent_id[indexPath.row]
    p_name = parent_name[indexPath.row]
 }
}

1 个答案:

答案 0 :(得分:1)

看来你在这次电话会议后没有重新加载:

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()之后添加self.loadIndicator.stopAnimating()