In my app, I'm showing user a table view of entries and when they tap on one, they're taken to a screen with a TextView so they can edit the contents of that cell. When they save, they're transitioned back to the tableView. The problem is that the data isn't reloading when they return to the tableView, it shows the same data as before they edited the contents of a cell
I've tried putting self.tableView.reloadData()
in both viewWillAppear
and viewDidAppear
but neither are working. I've read a bunch of answers pertaining to this and none have worked. I'd love your help. Thanks!
PastSessionsViewController.swift
class PastSessionsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var arrayOfEntriesNew: [Entry] = [Entry]()
let transitionManager = TransitionManager()
var entry: String?
var entryDate: NSDate?
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
let gradient: CAGradientLayer = CAGradientLayer()
let arrayColors: [AnyObject] = [
UIColor(red: 0.302, green: 0.612, blue: 0.961, alpha: 1.000).CGColor,
UIColor(red: 0.247, green: 0.737, blue: 0.984, alpha: 1.000).CGColor
]
tableView.backgroundColor = nil
tableView.separatorStyle = UITableViewCellSeparatorStyle.None
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 168.0
gradient.frame = view.bounds
gradient.colors = arrayColors
view.layer.insertSublayer(gradient, atIndex: 0)
prepareEntries()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(animated: Bool) {
self.tableView.reloadData()
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
}
func prepareEntries() {
let appDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
let managedObjectContext = appDelegate.managedObjectContext!
let request = NSFetchRequest(entityName: "Meditation")
let sortDescriptor = NSSortDescriptor(key: "date", ascending: false)
request.sortDescriptors = [sortDescriptor]
var error: NSError?
var showStreak = (top: false, bottom: false)
let result = managedObjectContext.executeFetchRequest(request, error: &error)
if let objects = result as? [Meditation] {
for (index, object) in enumerate(objects) {
var timeLabel = lengthMinutesLabel(minutesString: lengthMinutes(object.length))
var entry = Entry(sessionLength: lengthMinutes(object.length), sessionTimeUnit: timeLabel, sessionStartTime: "\(object.date.format())", sessionJournalEntry: object.journalEntry, sessionStreakTop: showStreak.top, sessionStreakBottom: showStreak.bottom)
arrayOfEntriesNew.append(entry)
}
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayOfEntriesNew.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: EntryCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as EntryCell
let entry = arrayOfEntriesNew[indexPath.row]
var journalEntry = entry.sessionJournalEntry
cell.sessionStartTimeLabel.text = entry.sessionStartTime
cell.sessionJournalEntryLabel.text = journalEntry
cell.sessionLengthLabel.text = entry.sessionLength
cell.sessionTimeUnitLabel.text = entry.sessionTimeUnit
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// janky but works, probably a better way to do this. Will figure it out after v1
if sender is UIButton {
let destinationVC = segue.destinationViewController as ViewController
destinationVC.showJournalButton = false
} else {
let path = self.tableView.indexPathForSelectedRow()!
let location = path.row
let appDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
let managedObjectContext = appDelegate.managedObjectContext!
let request = NSFetchRequest(entityName: "Meditation")
let sortDescriptor = NSSortDescriptor(key: "date", ascending: false)
request.sortDescriptors = [sortDescriptor]
var error: NSError?
let result = managedObjectContext.executeFetchRequest(request, error: &error)
if let objects = result as? [Meditation] {
var journalVC = segue.destinationViewController as EditJournalViewController
journalVC.journalEntryCoreDataLocation = path.row
journalVC.journalEntryToEdit = objects[location].journalEntry
journalVC.journalEntryToEditTimestamp = objects[location].date
let transitionManager = self.transitionManager
transitionManager.presenting = true
transitionManager.direction = "left"
journalVC.transitioningDelegate = transitionManager
}
}
}
}
在viewWillAppear
添加prepareEntries()
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.