简体   繁体   中英

Reloading UITableview on iOS

So when I select an item from the tableview, it segues into another viewcontroller and its passes data through that, I have it set that I segue back into the parent view controller. however it loses that data from the tableview, I tried to re-run the method that loads it but that doesn't seem to work, ideas as to what I am missing? I try to re-run methods that I think will do the job, but I'm not sure.

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


}

Maybe I am missing something?

Here's what I have tried

After thinking about when I should update the vListTableView: UITableView! , it makes sense that it should be updated immediately after populating vList . So it makes enough sense to me that the self.vListTableView.reloadData() should be called at the end of downloadDataFromDB as such:

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)
}

This way, reloaddata is always called at the end of downloadDataFromDB and it is never needed anywhere else.

Like clockwork.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM