swift:刷新/重新加载表视图不起作用

时间:2015-10-30 19:28:05

标签: tableview swift2 xcode7 reloaddata uirefreshcontrol

我是swift开发的新手,我在服务器中添加了数据,尝试使用refreshcontrol函数刷新tableviewcontroller,但表视图中的值没有改变。

类MainTableViewController:UITableViewController,UINavigationControllerDelegate {

@IBOutlet var sosTableView: UITableView!

var datas = [dataSos]()
override func viewDidLoad() {
    super.viewDidLoad()

    let spinningActivity = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
    spinningActivity.labelText = "Loading"
    spinningActivity.detailsLabelText = "Please wait"
    dispatch_async(dispatch_get_main_queue()) {
        self.loadDataServer()
        spinningActivity.hide(true)
        self.sosTableView.reloadData()
    }


    //loadDataSos()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()

  var refreshControl = UIRefreshControl()
    refreshControl.addTarget(self, action: Selector("refreshData"), forControlEvents: UIControlEvents.ValueChanged)
    self.refreshControl = refreshControl
}

刷新功能

func refreshData(){

    dispatch_async(dispatch_get_main_queue()) {
        self.loadDataServer()

        self.sosTableView.reloadData()
    }

    refreshControl?.endRefreshing()

}

加载服务器功能

func loadDataServer(){

    do {

        let data = NSData(contentsOfURL: NSURL(string: "http://xxxx/scripts/xxx.php")!)

        let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)




        //let NumberOfPersons = jsonResult.count

        // **LOOP THROUGH THE JSON ARRAY**
        for anItem in jsonResult as! [Dictionary<String, AnyObject>] {
            let userId = anItem["userId"] as! String
            let userName = anItem["firstName"] as! String
            let userAddress = anItem["address"] as! String
            let userDate = anItem["date"] as! String
            let userLocation = anItem["location"] as! String
            var userEvent = anItem["event"] as? String
            let sosId = anItem["sosId"] as! String
            // do something with personName and personID


             let imageUrl = NSURL(string:"http://xxx")
             let imageData = NSData(contentsOfURL: imageUrl!)


            if userEvent == nil{
                userEvent = "Need Help"

            }else if userEvent! == "1" {
                userEvent! = "Thief"
            }
            else if userEvent! == "2" {
                userEvent! = "Fire"
            }
            else{
                userEvent! = "Healthy Issue"
            }



            //print(personName)
            if imageData == nil{
                let photo1 = UIImage(named: "defaultPhoto")!
                let data1 = dataSos(userId: userId, name: userName, location: userLocation, address: userAddress, event: userEvent!, date: userDate, photo: photo1, sosId: sosId)
                self.datas += [data1]
            }
            else{
            let photo1 = UIImage(data: imageData!)
            //let photo1 = UIImage(named: "defaultPhoto")
                let data1 = dataSos(userId: userId, name: userName, location: userLocation, address: userAddress, event: userEvent!, date: userDate, photo: photo1, sosId: sosId)

            self.datas += [data1]
            }
        }






    } catch let error as NSError {

        print(error)

    }
   // }
}

更新:表格视图数据源

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // warning Incomplete implementation, return the number of rows
    return datas.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


    let cellIdentifier = "cell"

    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! MainTableViewCell

    // Configure the cell...
    let data0 = datas[indexPath.row]

    cell.nameLabel.text = data0.name
    cell.locationLabel.text = data0.location
    cell.addressTextView.text = data0.address
    cell.eventLabel.text = data0.event
    cell.dateLabel.text = data0.date
    cell.photoLabel.image = data0.photo
    self.roundingUIView(cell.photoLabel, cornerRadiusParam: 35)


    return cell



}

1 个答案:

答案 0 :(得分:0)

好的,我只知道你继承了UITableViewController,因此你已经从它继承了tableView属性。此属性的表视图已将delegate和dataSource设置为您的控制器,但不是您的自定义sosTableView。您应该使用继承的tableView属性替换自定义sosTableView,然后一切都会按照您的预期运行。