Wenn ich die Sammlungsansicht über den Bildschirm verschiebe, wird der gesamte Ansichtscontroller erneut geladen. Das bedeutet, dass alle zuvor aufgetretenen Animationen erneut passieren müssen. Gibt es eine Möglichkeit, den View-Controller beim Laden der Sammlungsansicht zu stoppen?Wenn ich horizontal durch meine Sammlungsansicht scrolle, wird die gesamte Ansicht neu geladen, wobei die Animation erneut gestartet wird.
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell2 = CollectionView.dequeueReusableCellWithReuseIdentifier("CollectionCell", forIndexPath: indexPath) as! BookingDateCollectionCellClass
let later = getDate(indexPath.row + 5).day
print(later)
let date: String = String(later)
print(date)
cell2.dateLabel.text = date
let day = getDateData(indexPath.row + 5).dayOfWeek()
print(day)
cell2.dayLabel.text = dayShortFromNumber(day!)
return cell2
}
import UIKit
class BookingDateVC: UIViewController, UITableViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource {
// Outlets
@IBOutlet weak var tableViewDates: UITableView!
@IBOutlet weak var CollectionView: UICollectionView!
@IBOutlet weak var bigMonthView: UIView!
@IBOutlet weak var selectDateLabel: UILabel!
@IBOutlet weak var todayDateView: UIView!
@IBOutlet weak var todayButton: UIButton!
@IBOutlet weak var tomorrowButton: UIButton!
@IBOutlet weak var laterButton: UIButton!
@IBOutlet weak var plusButton: UIButton!
@IBOutlet weak var monthView: UIView!
@IBOutlet weak var monthLabel: UILabel!
// Calendar Variables
var month: Int?
var index: NSIndexPath?
var passData: dataReceipt?
override func viewDidLoad() {
super.viewDidLoad()
// Table View Customerisation
tableViewDates.backgroundColor = UIColor(red: 38/255, green: 163/255, blue: 133/255, alpha: 1)
self.tableViewDates.center.y += self.view.bounds.height
}
override func viewDidAppear(animated: Bool) {
// Sets up inital dates - Today, Tomorrow, Later
let today = getDate(0).day
let tomorrow = getDate(1).day
let later = getDate(2).day
todayButton.setTitle(String(today), forState: .Normal)
tomorrowButton.setTitle(String(tomorrow), forState: .Normal)
laterButton.setTitle(String(later), forState: .Normal)
plusButton.setTitle("+", forState: .Normal)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func todayButton(sender: AnyObject) {
passData?.data = monthFromNumber(getDate(0).month) + " " + todayButton.currentTitle!
}
@IBAction func tomorrowButton(sender: AnyObject) {
passData?.data = monthFromNumber(getDate(1).month) + " " + tomorrowButton.currentTitle!
}
// Gets date as components - Hour, Day, Month, Year
func getDate(days: Int) -> NSDateComponents{
let date = getDateData(days)
let unitFlags: NSCalendarUnit = [.Hour, .Day, .Month, .Year]
let components = NSCalendar.currentCalendar().components(unitFlags, fromDate: date)
return components
}
// Gets date + number of days from today
func getDateData(days: Int) -> NSDate{
let today = NSDate()
let date = NSCalendar.currentCalendar().dateByAddingUnit(
.Day,
value: days,
toDate: today,
options: NSCalendarOptions(rawValue: 0))
return date!
}
// Returns day string in full from day int
func dayFromNumber(day: Int) -> String{
switch(day){
case 1:
return "SUNDAY"
case 2:
return "MONDAY"
case 3:
return "TUESDAY"
case 4:
return "WEDNESDAY"
case 5:
return "THURSDAY"
case 6:
return "FRIDAY"
case 7:
return "SATURDAY"
default:
return ""
}
}
// Returns day string in short from day int
func dayShortFromNumber(day: Int) -> String{
switch(day){
case 1:
return "SUN"
case 2:
return "MON"
case 3:
return "TUE"
case 4:
return "WED"
case 5:
return "THU"
case 6:
return "FRI"
case 7:
return "SAT"
default:
return ""
}
}
// Returns month string in short from month int
func monthFromNumber(month: Int) -> String{
switch(month){
case 1:
return "JAN"
case 2:
return "FEB"
case 3:
return "MAR"
case 4:
return "APR"
case 5:
return "MAY"
case 6:
return "JUN"
case 7:
return "JUL"
case 8:
return "AUG"
case 9:
return "SEP"
case 10:
return "OCT"
case 11:
return "NOV"
case 12:
return "DEC"
default:
return ""
}
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 3
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableViewDates.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! BookingDateCellClass
let later = getDate(2).day
let date: String = String(indexPath.row + later)
cell.dateLabel.text = date
let day = getDateData(indexPath.row + 2).dayOfWeek()
cell.dayLabel.text = dayFromNumber(day!)
let bgColorView = UIView()
bgColorView.backgroundColor = UIColor(red: 253/255, green: 196/255, blue: 0, alpha: 1)
cell.selectedBackgroundView = bgColorView
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath) as! BookingDateCellClass
passData?.data = monthFromNumber(getDate(indexPath.row + 2).month) + " " + cell.dateLabel.text!
performSegueWithIdentifier("timeSegue", sender: self)
}
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return false
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return 60.0;//Choose your custom row height
}
@IBAction func laterButtonFunc(sender: AnyObject) {
// Animates to later dates
UIView.animateWithDuration(0.5, animations: {
self.tableViewDates.center.y = self.view.frame.height/2
self.todayDateView.center.y = self.view.frame.height/6
self.monthView.center.y = (self.view.frame.height/4) * 3.25
})
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 20
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell2 = CollectionView.dequeueReusableCellWithReuseIdentifier("CollectionCell", forIndexPath: indexPath) as! BookingDateCollectionCellClass
let later = getDate(indexPath.row + 5).day
let date: String = String(later)
cell2.dateLabel.text = date
let day = getDateData(indexPath.row + 5).dayOfWeek()
cell2.dayLabel.text = dayShortFromNumber(day!)
//self.monthLabel.text = monthFromNumber(getDate(indexPath.row + 5).month)
return cell2
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let cell = collectionView.cellForItemAtIndexPath(indexPath) as! BookingDateCollectionCellClass
passData?.data = monthFromNumber(getDate(indexPath.row + 5).month) + " " + cell.dateLabel.text!
performSegueWithIdentifier("timeSegue", sender: indexPath)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "timeSegue" {
if let destination = segue.destinationViewController as? BookingTimeVC {
print((passData?.data)!)
destination.passData = passData
}
}
}
}
Der View-Controller sollte nicht geladen werden, wenn die Sammlungsansicht neu geladen wird. Wenn dies der Fall ist, tun Sie das in Ihrem Code. Sie müssen eine detailliertere Beschreibung der Sequenz angeben, die das erneute Laden auslöst, einschließlich des aufgerufenen Codes. Ich schlage vor, Sie setzen einen Haltepunkt in der Methode, die wiederholt aufgerufen wird, und schauen auf den Aufruf-Stapel, um zu sehen, wie es erneut aufgerufen wird. –
Ich habe den vollen Quellcode hinzugefügt @DuncanC – User