在iOS上重新加载UITableview

时间:2016-09-07 06:48:15

标签: ios swift uitableview

因此,当我从tableview中选择一个项目时,它会进入另一个视图控制器并通过它传递数据,我将它设置为我将其转换回父视图控制器。但是它从tableview中丢失了这些数据,我试图重新运行加载它的方法,但这似乎不起作用,关于我缺少什么的想法?我尝试重新运行我认为可以完成工作的方法,但我不确定。

var locationManager = CLLocationManager()
var point = PFGeoPoint(latitude: 0.0, longitude: 0.0)
var vList = [Details]()
@IBOutlet var vListTableView: UITableView!
@IBOutlet var map: MKMapView!




override func viewDidLoad() {
    super.viewDidLoad()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()
    locationManager.startUpdatingLocation()

}



override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
    let cell = self.vListTableView.dequeueReusableCell(withIdentifier: "venueDetailCell",for: indexPath) as! DetailsTableViewCell

    cell.Distance.text = String(vList[indexPath.row].distance)
    cell.title.text = vList[indexPath.row].name

    return cell
}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return vList.count

}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    downloadDataFromDB(location: point)
    self.vListTableView.reloadData()
    locationManager.startUpdatingLocation()
}

override func prepare(for segue: UIStoryboardSegue, sender: AnyObject?) {
    if(segue.identifier == "vDetailToCommentList"){
        var selectedRowIndex = self.vListTableView.indexPathForSelectedRow
        let moveViewCont:CommentsViewController = segue.destination as! CommentsViewController
        moveViewCont.Test = "Data pass successful"
    }
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){

    let userLocation: CLLocation = locations[0]
    let latitude: CLLocationDegrees = userLocation.coordinate.latitude
    let longitude: CLLocationDegrees = userLocation.coordinate.longitude
    let latDelta: CLLocationDegrees = 0.05
    let lonDelta: CLLocationDegrees = 0.05
    let span: MKCoordinateSpan=MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: lonDelta)
    let location: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
    let region: MKCoordinateRegion = MKCoordinateRegion(center: location, span: span)
    map.setRegion(region, animated: true)
    map.showsUserLocation = true
    point = PFGeoPoint(latitude:latitude, longitude:longitude)
    updateUserLocationinDB(location: point)
    downloadDataFromDB(location: point)
}




func updateUserLocationinDB(location: PFGeoPoint){
    let uAct = PFQuery(className:"uAct")
    uAct.whereKey("userId", equalTo:(PFUser.current()?.username)!)
    uAct.findObjectsInBackground(block: {(objects, error) in
        if error != nil{
            print(error)
        }else if let users = objects {
            if objects?.count>0{
                for objects in users{
                    objects["UserLocation"] = location
                    objects.saveInBackground()
                }
            }

        }

    })
}


func downloadDataFromDB(location: PFGeoPoint){
    locationManager.stopUpdatingLocation()
    vList.removeAll()
    let qHotSpots = PFQuery(className: "HotSpots")
    qHotSpots.whereKey("venueLocation", nearGeoPoint: location, withinMiles: 10)
    do{
        let qReply = try qHotSpots.findObjects()
        if qReply.count>10{

            for object in qReply{
                let curDetails:Details = Details()
                let name:String = object.object(forKey:"venue")! as! String
                let id:String = object.objectId!
                let distance:Double = 0.0
                curDetails.name = name
                print("vList size",self.vList.count)
            }

        }
        else if qReply.count == 0{

            //TODO =: Download from API

        }
    }
    catch{
        print(error)
    }
}

class Details {
var id:String = ""
var name:String = ""
var distance:Double = 0.0


func Details(iD: String,nam: String,dist: Double){

    self.id = iD
    self.name = nam
    self.distance = dist


}

也许我错过了什么?

Here's what I have tried

1 个答案:

答案 0 :(得分:0)

在考虑何时应该更新vListTableView: UITableView!后,有意义的是应该在填充vList后立即更新。所以我觉得self.vListTableView.reloadData()应该在downloadDataFromDB的末尾调用,这样就足够了:

func downloadDataFromDB(location: PFGeoPoint){
locationManager.stopUpdatingLocation()
vList.removeAll()
let qHotSpots = PFQuery(className: "HotSpots")
qHotSpots.whereKey("venueLocation", nearGeoPoint: location, withinMiles: 10)
do{
    let qReply = try qHotSpots.findObjects()
    if qReply.count>10{

        for object in qReply{
            let curDetails:Details = Details()
            let name:String = object.object(forKey:"venue")! as! String
            let id:String = object.objectId!
            let distance:Double = 0.0
            curDetails.name = name
            //print("vList size",self.vList.count)
        }
      //THE FIX WAS HERE
      self.venueListTableView.reloadData()
    }
    else if qReply.count == 0{

        //TODO =: Download from API

    }
}
catch{
    print(error)
}

这样,reloaddata总是在downloadDataFromDB的末尾调用,在其他任何地方都不需要。

像发条一样。