认为哪个观点加倍,迅速

时间:2016-01-20 11:37:48

标签: ios swift uitableview uiviewcontroller segue

我用Foursquare API获取了我周围的商店。我想获取所选行的数据以在另一个ViewController中使用。

例如,当我选择一行时,第一个上传视图显示为空(对于第一个选择)或最后一个选择的日期,并立即显示另一个uploadView,其中包含所选行的数据(右侧) 。当我想回到TableView时,我必须在“< 商店”上点击两次(我浏览了两个上传视图,以及上传视图之间的数据) tableview和带有良好数据的上传视图仍然是由错误的数据组成的。

您知道如何避免重复上传吗?

Row 1 selected, upload view of the middle, composed with the data of my past selection, row 5 storyboard

在表格视图中:

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, CLLocationManagerDelegate {

@IBOutlet var tableView:UITableView!

var locationManager:CLLocationManager?;

var lastLocation:CLLocation?;

var venues:[Venue]?;

var FirstsendSelectedData = NSString()
var SecondsendSelectedData = NSString()
var ThirdsendSelectedData = NSString()
var FourthsendSelectedData = NSString()

let distanceSpan:Double = 1000; //500

var searchActive : Bool = false
var filtered = [Venue]()

override func viewDidLoad()
{
    super.viewDidLoad();
    self.tableView!.delegate = self
    self.tableView!.dataSource = self
    self.tableView!.rowHeight = UITableViewAutomaticDimension
    self.tableView!.estimatedRowHeight = 100


    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("onVenuesUpdated:"), name: API.notifications.venuesUpdated, object: nil);

}

func refreshVenues(location: CLLocation?, getDataFromFoursquare:Bool = false)
{
    if location != nil
    {
        lastLocation = location;
    }

    if let location = lastLocation
    {
        if getDataFromFoursquare == true
        {
            CoffeeAPI.sharedInstance.getCoffeeShopsWithLocation(location);
        }

        let (start, stop) = calculateCoordinatesWithRegion(location);

        let predicate = NSPredicate(format: "latitude < %f AND latitude > %f AND longitude > %f AND longitude < %f", start.latitude, stop.latitude, start.longitude, stop.longitude);

        let realm = try! Realm();

        venues = realm.objects(Venue).filter(predicate).sort {
            location.distanceFromLocation($0.coordinate) > location.distanceFromLocation($1.coordinate);
        }

        tableView?.reloadData();
    }
} 

func calculateCoordinatesWithRegion(location:CLLocation) -> (CLLocationCoordinate2D, CLLocationCoordinate2D)
{
    let region = MKCoordinateRegionMakeWithDistance(location.coordinate, distanceSpan, distanceSpan);

    var start:CLLocationCoordinate2D = CLLocationCoordinate2D();
    var stop:CLLocationCoordinate2D = CLLocationCoordinate2D();

    start.latitude  = region.center.latitude  + (region.span.latitudeDelta  / 2.0);
    start.longitude = region.center.longitude - (region.span.longitudeDelta / 2.0);
    stop.latitude   = region.center.latitude  - (region.span.latitudeDelta  / 2.0);
    stop.longitude  = region.center.longitude + (region.span.longitudeDelta / 2.0);

    return (start, stop);
}

override func viewWillAppear(animated: Bool)
{
    super.viewWillAppear(animated);

    if let tableView = self.tableView
    {
        tableView.delegate = self;
        tableView.dataSource = self;
    }
}


override func viewDidAppear(animated: Bool)
{
    if locationManager == nil
    {
        locationManager = CLLocationManager();

        locationManager!.delegate = self;
        locationManager!.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
        locationManager!.requestAlwaysAuthorization();  //Pop-ups turn on geo-location
        locationManager!.distanceFilter = 50; // Don't send location updates with a distance smaller than 50 meters between them
        locationManager!.startUpdatingLocation();
    }
}

func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation)
{
    if let tableView = self.tableView
    {
        let region = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, distanceSpan, distanceSpan);

        refreshVenues(newLocation, getDataFromFoursquare: true)
    }
}

func onVenuesUpdated(notification:NSNotification)
{
    refreshVenues(nil)
}

func tableView(tableView:UITableView,numberOfRowsInSection section:Int) - &gt; Int {         返回场地?.count ?? 0     }

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

let cell = tableView.dequeueReusableCellWithIdentifier("BusinessTableViewCell") as? BusinessTableViewCell

    let venue = venues?[indexPath.row]

    cell!.TextLabel!.text = "\(indexPath.row + 1). \(venue!.name)"
    cell!.DetailTextLabel?.text = venue!.address;
    cell!.longTextLabel?.text = String(venue!.longitude);
    cell!.latTextLabel?.text = String(venue!.latitude);

    print(venue!.name , venue!.address, venue!.latitude, venue!.longitude)

let distance = venue!.coordinate.distanceFromLocation((self.lastLocation)!)
cell!.distanceLabel.text = String(format: "%.1f mi", distance / 1609.344)
cell!.distanceLabel.sizeToFit()

    print(distance)

cell!.layoutIfNeeded()

return cell!
}

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

    print("You selected cell #\(indexPath.row)!")

    // Get Cell Label text here and storing it to the variable
    let indexPathVal: NSIndexPath = tableView.indexPathForSelectedRow!
    print("\(indexPathVal)")
    let currentCell = tableView.cellForRowAtIndexPath(indexPathVal) as! BusinessTableViewCell;
    print("\(currentCell)")
    print("\(currentCell.TextLabel?.text!)")
    print("\(currentCell.DetailTextLabel?.text!)")
    print("\(currentCell.longTextLabel?.text!)")
    print("\(currentCell.latTextLabel?.text!)")
    //Storing the data to a string from the selected cell
    FirstsendSelectedData = (currentCell.TextLabel?.text)!
    SecondsendSelectedData = (currentCell.DetailTextLabel?.text)!
    ThirdsendSelectedData = (currentCell.longTextLabel?.text)!
    FourthsendSelectedData = (currentCell.latTextLabel?.text)!

    print(FirstsendSelectedData)
    print(SecondsendSelectedData)
    print(ThirdsendSelectedData)
    print(FourthsendSelectedData)
    //Now here I am performing the segue action after cell selection to the other view controller by using the segue Identifier Name
   self.performSegueWithIdentifier("Upload", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    //Here i am checking the Segue and Saving the data to an array on the next view Controller also sending it to the next view Controller
    if segue.identifier == "Upload"{
        //Creating an object of the second View controller
    let controller = segue.destinationViewController as! UploadViewController
        //Sending the data here

        controller.FirstArray = FirstsendSelectedData as String
        controller.SecondArray = SecondsendSelectedData as String
        controller.ThirdArray = ThirdsendSelectedData as String
        controller.FourthArray = FourthsendSelectedData as String
    }
  }  
}

并在我的UploadViewController中:

class UploadViewController: UIViewController, UIImagePickerControllerDelegate,
    UINavigationControllerDelegate {

    // outlet 
    @IBOutlet weak var TextLabel : UILabel!
    @IBOutlet weak var DetailTextLabel : UILabel!

     var FirstArray:String!
     var SecondArray:String!
     var ThirdArray:String!
     var FourthArray:String!


    // MARK: - View functions


    override func viewDidLoad() {
        super.viewDidLoad()

         TextLabel?.text = FirstArray!
         DetailTextLabel?.text = SecondArray!
         print ( ThirdArray, FourthArray)
  }
}

0 个答案:

没有答案