2017-10-23 7 views
0

Ich habe zwei ViewControllers. Der erste mit einem Stepper:UiTableView Anzahl der generierten Zellen

var steppers : UIStepper? 

@IBOutlet weak var hourLabel: UILabel! 
@IBOutlet weak var stepper: UIStepper! 

@IBAction func stepper(_ sender: UIStepper) { 
    hourLabel.text = String(sender.value) 
} 

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if let np = segue.destination as? CourseClass2 { 
     np.numberPlaces = Int(stepper.value) 
    } 
} 

und die zweite mit einem Tableview:

private let resueIdentifier = "MyTableViewCell" 

extension UIViewController { 
    func present(viewController : UIViewController, completion : (() ->())? = nil){ 
     if let presented = self.presentedViewController { 
      presented.dismiss(animated: true, completion: { 
       self.present(viewController, animated: true, completion: completion) 
      }) 
     } else { 
      self.present(viewController, animated: true, completion: completion) 
     } 
    } 
} 

class CourseClass2: UIViewController, UITableViewDelegate, UITableViewDataSource { 
    @IBOutlet weak var tableView: UITableView! 

    var locationManager:CLLocationManager? 
    let minimumSpacing : CGFloat = 15 //CGFloat(MAXFLOAT) 
    let cellWidth: CGFloat = 250 
    let radius = 5000 // 5km 
    var category : QCategoryy? 
    var currentLocation : CLLocationCoordinate2D? 
    var places: [QPlace] = [] 
    var isLoading = false 
    var response : QNearbyPlacesResponse? 
    var rows = 0 
    var numberPlaces = 0 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     self.title = category?.name 
     tableView.dataSource = self 
     tableView.delegate = self 
    } 

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

     determineMyCurrentLocation() 
    } 

    override func viewWillDisappear(_ animated: Bool) { 
     super.viewWillDisappear(animated) 
    } 

    override func viewDidAppear(_ animated: Bool) { 
     super.viewDidAppear(animated) 

     rows = 0 
    /* insertRowsMode3() 
     tableView.reloadData() */ 
     category?.markView() 
    } 

    @IBAction func refreshTapped(_ sender: Any) { 
     rows = 0 
    /* insertRowsMode3() */ 
     tableView.reloadData() 
    } 

    func canLoadMore() -> Bool { 
     if isLoading { 
      return false 
     } 

     if let response = self.response { 
      if (!response.canLoadMore()) { 
       return false 
      } 
     } 

     return true 
    } 

    func loadPlaces(_ force:Bool) { 
     if !force { 
      if !canLoadMore() { 
       return 
      } 
     } 

     print("load more") 
     isLoading = true 
     NearbyPlaces.getNearbyPlaces(by: category?.name ?? "food", coordinates: currentLocation!, radius: radius, token: self.response?.nextPageToken, completion: didReceiveResponse) 
    } 

    func didReceiveResponse(response:QNearbyPlacesResponse?, error : Error?) -> Void { 
     if let error = error { 
      let alertController = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: .alert) 
      let actionDismiss = UIAlertAction(title: "Dismiss", style: .cancel, handler: nil) 
      let actionRetry = UIAlertAction(title: "Retry", style: .default, handler: { (action) in 
       DispatchQueue.main.async { 
        self.loadPlaces(true) 
       } 
      }) 
      alertController.addAction(actionRetry) 
      alertController.addAction(actionDismiss) 
      DispatchQueue.main.async { 
       self.present(viewController: alertController) 

      } 
     } 
     if let response = response { 
      self.response = response 
      if response.status == "OK" { 
       if let placesDownloaded = response.places { 
        places.append(contentsOf: placesDownloaded) 
       } 

       self.tableView?.reloadData() 
      } else { 
       let alert = UIAlertController.init(title: "Error", message: response.status, preferredStyle: .alert) 
       alert.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil)) 
       alert.addAction(UIAlertAction.init(title: "Retry", style: .default, handler: { (action) in 
        DispatchQueue.main.async { 
         self.loadPlaces(true) 
        } 
       })) 
       self.present(viewController: alert) 
      } 
      isLoading = false 
     } 
     else { 
      print("response is nil") 
     } 
    } 

    func numberOfSections(in tableView: UITableView) -> Int { 
     print("numberOfsection Call") 
     return 1 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     print("numberOfRows Call") 
     return places.count /* rows */ 
    } 

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: resueIdentifier, for: indexPath) as! MyTableViewCell 

     let place = places[indexPath.row] 
     cell.update(place: place) 

     if indexPath.row == places.count - 1 { 
      loadPlaces(false) 
     } 
     print("CellForRow Call") 
     return (cell) 
    } 

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     tableView.deselectRow(at: indexPath, animated: true) 

     UIView.animate(withDuration: 0.2, animations: { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "MyTableViewCell", for: indexPath) as! MyTableViewCell 
    }) 

     performSegue(withIdentifier: "goToLast" , sender: indexPath.row) 
    } 

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
     return 100 
    } 

    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 
     return true 
    } 

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
     if editingStyle == UITableViewCellEditingStyle.delete { 
      places.remove(at: indexPath.row) 
      tableView.deleteRows(at: [indexPath], with: .fade) 
     } 
    } 

    func didReceiveUserLocation(_ userLocation:CLLocation) { 
     currentLocation = userLocation.coordinate 

     loadPlaces(true) 
    } 

Ich habe nur den Code, die für die Frage. Wie man sehen kann ich von der ersten VC auf den zweiten, diesen Wert zu übergeben bin versucht:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if let np = segue.destination as? CourseClass2 { 
     np.numberPlaces = Int(stepper.value) 
    } 
} 

Weil ich eine Grenze für die Anzahl der Zellen hinzufügen mag, die laichen, wenn der Tableview in dem zweiten Viewcontroller geladen (und ich möchte, dass dieser Grenzwert vom Benutzer mit dem Stepper im vorherigen Controller gewählt wird), wie muss ich die TabelleView ändern, um sicherzustellen, dass die Anzahl der erzeugten Zellen dem Wert var numberPlaces entspricht?

+0

Verwenden Sie den Inhalt von 'Orte' als Hintergrund für die Tabellenansicht? –

+0

@PhillipMills ja – seran

Antwort

0

Ich würde vorschlagen, dass Sie die Anzahl der Zellen begrenzen innerhalb UITableView Delegatmethode Anzahl der Zellen

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    print("numberOfRows Call") 
    if places.count < self.numberPlaces { 
     return places.count /* rows */ 
    } 
    return self.numberPlaces 
} 

Auf diese Weise werden Sie richtige Anzahl von Zellen erhalten, aber wenn es wäre zu viele Orte nur Selbst .numberPlaces werden angezeigt.

Vielleicht müssten Sie einige Sortierung herausfinden, damit die richtigen Orte angezeigt werden. (Von Nähe, ...)

+0

Ich habe versucht, aber mit dieser Änderung der TabelleView nichts anzeigen, jedenfalls immer die places.count wird höher sein als numberPlaces – seran

+0

ich brauche nur, dass die places.count ist = zu numberPlaces – seran

+0

Bitten, könnten Sie bestätigen, dass Innerhalb dieser Methode erhalten Sie den richtigen Einstellwert self.numberPlaces. –

Verwandte Themen