在我的应用程序中,我向用户显示条目的表格视图,当他们点击一个条目时,他们将被带到带有TextView的屏幕,以便他们可以编辑该单元格的内容。当他们保存时,他们会转换回tableView。 问题是数据在返回tableView时没有重新加载,它显示与编辑单元格内容之前相同的数据
我尝试将self.tableView.reloadData()
放在viewWillAppear
和viewDidAppear
中,但两者都没有效果。我已经阅读了一些与此有关的答案,但没有一个有效。我很乐意帮助你。谢谢!
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
}
}
}
}
答案 0 :(得分:0)
在prepareEntries()
viewWillAppear