Ios 在领域数据库更改后重新加载TableView
我正在Swift中构建一个项目管理应用程序,使用Realm作为数据库来存储我的项目。Ios 在领域数据库更改后重新加载TableView,ios,swift,uitableview,realm,Ios,Swift,Uitableview,Realm,我正在Swift中构建一个项目管理应用程序,使用Realm作为数据库来存储我的项目。 此WeekViewController是我的初始视图控制器;它包含一个UITableView,以显示本周到期的项目。使用条形按钮,用户可以转到AddProjectViewController,顾名思义,它用于创建新项目并将它们保存到领域数据库添加项目视图控制器以模式显示 输入所需的项目详细信息后,用户可以单击“保存”按钮,将项目保存到领域数据库并取消视图,返回到ThisWeekViewController。但是
此WeekViewController
是我的初始视图控制器;它包含一个UITableView
,以显示本周到期的项目。使用条形按钮,用户可以转到AddProjectViewController
,顾名思义,它用于创建新项目并将它们保存到领域数据库<代码>添加项目视图控制器以模式显示
输入所需的项目详细信息后,用户可以单击“保存”按钮,将项目保存到领域数据库并取消视图,返回到ThisWeekViewController
。但是,我在更新TableView以反映新项目的添加时遇到了问题
在查阅了Realm文档之后,我现在了解到,在Realm中,在数据库发生更改后,无需手动从TableView中添加或删除行。相反,您应该使用通知处理程序。但是,我不太确定在关闭AddProjectViewController
后如何以及在何处合并它以重新加载我的TableView
在ThisWeekViewController
的视图中调用处理程序将出现
方法,因为视图实际上从未消失,因为添加项目视图控制器
是以模式显示的
AddProjectViewController
:
class AddProjectViewController: FormViewController {
// Realm Initialization
let realm = try! Realm()
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.rightBarButtonItem?.isEnabled = false
form +++ Section()
<<< TextRow(){ row in
row.placeholder = "Postname"
row.tag = "ProjectName"
}.onChange({ (row) in
if row.cell.textField.hasText {
self.navigationItem.rightBarButtonItem?.isEnabled = true
} else {
self.navigationItem.rightBarButtonItem?.isEnabled = false
}
})
<<< TextAreaRow() { row in
row.placeholder = "Notizen"
row.textAreaHeight = .fixed(cellHeight: 240.0)
row.tag = "ProjectNotes"
}
form +++ Section()
<<< DateTimeInlineRow() { row in
row.title = "Fällig am"
row.value = Date(timeIntervalSinceNow: 0)
row.minuteInterval = 15
row.tag = "ProjectDueDate"
}
navigationOptions = RowNavigationOptions.Enabled.union(.StopDisabledRow)
animateScroll = true
rowKeyboardSpacing = 20
}
// MARK: - User defined functions
// If user presses the cancel button, the view is dismissed from screen.
@IBAction func cancelButtonPressed(_ sender: UIBarButtonItem) {
dismiss(animated: true) {}
}
// If user presses the save button, a new Project() item is created and saved to the Realm database.
@IBAction func saveButtonPressed(_ sender: UIBarButtonItem) {
// print(form.values())
let newProject = Project()
let titleRow: TextRow? = form.rowBy(tag: "ProjectName")
let projectName = titleRow?.value
newProject.title = projectName!
let notesRow: TextAreaRow? = form.rowBy(tag: "ProjectNotes")
let projectNotes = notesRow?.value
newProject.notes = projectNotes
let dueDateRow: DateTimeInlineRow? = form.rowBy(tag: "ProjectDueDate")
let projectDueDate = dueDateRow?.value
newProject.dueDate = projectDueDate
newProject.dateCreated = NSDate.now
print(newProject)
// Save the new Project to the realm database
do {
try self.realm.write {
realm.add(newProject)
}
}
catch {
print("Error saving item to Realm database. \(error)")
}
print(newProject)
self.dismiss(animated: true)
}
}
class ThisWeekViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
// MARK: - Variables
@IBOutlet weak var thisWeekTableView: UITableView!
// Realm initialization
let realm = try! Realm()
override func viewDidLoad() {
super.viewDidLoad()
setupTableView()
print(Realm.Configuration.defaultConfiguration.fileURL!)
}
// MARK: - Data Source / Delegate Methods
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 7
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//TODO: Handle user selection of a specific planned post
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Variables
let thisWeekCell = thisWeekTableView.dequeueReusableCell(withIdentifier: ThisWeekTableViewCell.reuseIdentifier()) as! ThisWeekTableViewCell
let today = Date()
let day = Calendar.current.date(byAdding: .day, value: indexPath.row, to: today)
let dayNumber = Calendar.current.component(.day, from: day!)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEE"
let dayName = dateFormatter.string(from: day!)
let startOfToday = Calendar.current.startOfDay(for: day!)
let endOfToday = Calendar.current.date(byAdding: .second, value: 86399, to: startOfToday)!
let projectNamesForCurrentDay = projectNamesForDay(startOfDay: startOfToday, endOfDay: endOfToday)
thisWeekCell.setupProjectNameLabel(projectNames: projectNamesForCurrentDay)
if indexPath.row == 0 {
thisWeekCell.dayNumberLabel.textColor = UIColor.orange
thisWeekCell.dayTextLabel.text = "Heute (\(dayName))"
}
if indexPath.row == 1 {
thisWeekCell.dayTextLabel.text = "Morgen (\(dayName))"
}
if indexPath.row > 1 {
thisWeekCell.dayTextLabel.text = dayName
}
thisWeekCell.dayNumberLabel.text = String(dayNumber)
return thisWeekCell
}
// MARK: - User definded functions
/**
Initial setup for the TableView. Registers all neccessary custom cells, sets the delegate and dataSource, and enables AutoLayout.
*/
func setupTableView() {
thisWeekTableView.register(UINib(nibName: ThisWeekTableViewCell.nibName(), bundle: nil), forCellReuseIdentifier: ThisWeekTableViewCell.reuseIdentifier())
thisWeekTableView.delegate = self
thisWeekTableView.dataSource = self
thisWeekTableView.rowHeight = UITableView.automaticDimension
thisWeekTableView.estimatedRowHeight = 54.0
}
/**
Takes two boundary objects of type Date and returns an array of strings containing the titles of all projects in the database that have a due date between the boundary dates.
- Parameter startOfDay: The start-of-day boundary object
- Parameter endOfDay: The end-of-day boundary object
- Returns: An array of strings containing project titles
*/
func projectNamesForDay(startOfDay: Date, endOfDay: Date) -> [String] {
let filteredProjects = realm.objects(Project.self).filter("dueDate BETWEEN %@", [startOfDay, endOfDay])
var projectNames = [String]()
for project in filteredProjects {
projectNames.append(project.title)
}
return projectNames
}
}
每次项目结果更改时,应使用领域通知自动更新tableview:
// Observe Realm Notifications
let token = projectResult.observe { notification, realm in
// reloadTableView
}
// later
token.invalidate()
每次项目结果更改时,应使用领域通知自动更新tableview:
// Observe Realm Notifications
let token = projectResult.observe { notification, realm in
// reloadTableView
}
// later
token.invalidate()
我明白,我只是不确定这段代码应该放在我的ViewController中的什么地方。将其放在ViewDidLoad或ViewWillExample中不会产生预期的结果。我认为您应该将其放在ViewDidLoad中,并在项目结果更改时在回调中加载新数据+重新加载tableView,并在deinit()中使令牌无效。我理解,我只是不确定这段代码应该放在我的ViewController中的何处。将其放置在ViewDidLoad或ViewWillExample中不会产生预期的结果。我认为您应该将其放置在ViewDidLoad中,并在项目结果更改时在回调中加载新数据+重新加载tableView,并在deinit()中使令牌无效。