我无法从UIAlertController
刷新tableView数据。
该代码适用于测验风格的应用程序,此页面允许用户选择
科目以及其他一些选项(见截图)。有一个
"旁边的重置按钮;仅显示未见问题"触发一个
UIAlertController
。但是,单击此警报中的“重置”操作
更新数据库但不更新tableView。数据库是
肯定更新,好像我回到页面,然后重新访问
tableView,更新主题单元格中看不见的问题值。我意识到这里有很多
这些类型的问题在这里,但我担心通常的修复都没有
工作
额外信息:
到目前为止,我有:
print(self.tableView.datasource)
等reloadData()
正在解雇reloadData()
下面提供TableViewController代码和截图。
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.dataSource = self
self.tableView.delegate = self
//For unique question picker changed
NotificationCenter.default.addObserver(self, selector: #selector(SubjectsTableViewController.reloadView(_:)), name:NSNotification.Name(rawValue: "reload"), object: nil)
//For slider value changed
NotificationCenter.default.addObserver(self, selector: #selector(SubjectsTableViewController.updateQuantity(_:)), name:NSNotification.Name(rawValue: "updateQuantity"), object: nil)
//Trigger UIAlertController
NotificationCenter.default.addObserver(self, selector: #selector(SubjectsTableViewController.showAlert(_:)), name:NSNotification.Name(rawValue: "showAlert"), object: nil)
}
// MARK: - Table view data source
///////// Sections and Headers /////////
override func numberOfSections(in tableView: UITableView) -> Int {
return 3
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{
let subjectHeaderCell = tableView.dequeueReusableCell(withIdentifier: "sectionHeader")
switch section {
case 0:
subjectHeaderCell?.textLabel?.text = "Select Subjects"
return subjectHeaderCell
case 1:
subjectHeaderCell?.textLabel?.text = "Options"
return subjectHeaderCell
case 2:
subjectHeaderCell?.textLabel?.text = ""
return subjectHeaderCell
default:
subjectHeaderCell?.textLabel?.text = ""
return subjectHeaderCell
}
}
//Header heights
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
{
return 34.0
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0:
return SubjectManager.subjectWorker.countSubjects()
case 1:
return 2
case 2:
return 1
default:
return 0
}
}
///////// Rows within sections /////////
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch (indexPath.section) {
case 0:
//Configure subjectCell //
let cellWithSubject = tableView.dequeueReusableCell(withIdentifier: "subjectCell", for: indexPath) as! SubjectTableViewCell
//Curve corners
cellWithSubject.subjectCellContainer.layer.cornerRadius = 2
cellWithSubject.subjectCellContainer.layer.masksToBounds = true
//Set subject title label
cellWithSubject.subjectTitleLabel.text = SubjectManager.subjectWorker.collateSubjectTitles()[indexPath.row]
//Available questions for subject label
questionCountForSubjectArray = QuestionManager.questionWorker.countQuestions()
cellWithSubject.subjectAvailableQuestionsLabel.text = "Total questions available: \(questionCountForSubjectArray[indexPath.row])"
//Get questions in subject variables
seenQuestionsForSubjectArray = QuestionManager.questionWorker.countOfQuestionsAlreadySeen()
//New questions available label
unseenQuestionsForSubjectArray.append(questionCountForSubjectArray[indexPath.row] - seenQuestionsForSubjectArray[indexPath.row])
cellWithSubject.newQuestionsRemainingLabel.text = "New questions remaining: \(unseenQuestionsForSubjectArray[indexPath.row])"
return cellWithSubject
case 1:
switch (indexPath.row) {
case 0:
//Configure uniqueQuestionCell //
let cellWithSwitch = tableView.dequeueReusableCell(withIdentifier: "uniqueQuestionCell", for: indexPath) as! UniqueQuestionTableViewCell
//Curve corners
cellWithSwitch.uniqueQuestionContainer.layer.cornerRadius = 2
cellWithSwitch.uniqueQuestionContainer.layer.masksToBounds = true
return cellWithSwitch
case 1:
//Configure sliderCell //
let cellWithSlider = tableView.dequeueReusableCell(withIdentifier: "questionPickerCell", for: indexPath) as! QuestionPickerTableViewCell
//Curve corners
cellWithSlider.pickerCellContainer.layer.cornerRadius = 2
cellWithSlider.pickerCellContainer.layer.masksToBounds = true
//Set questions available label
cellWithSlider.questionsAvailableLabel.text = "Available: \(sumQuestionsSelected)"
//Configure slider
cellWithSlider.questionPicker.maximumValue = Float(sumQuestionsSelected)
cellWithSlider.questionPicker.isContinuous = true
//Logic for if available questions changes - updates slider stuff
if questionQuantityFromSlider > sumQuestionsSelected {
questionQuantityFromSlider = sumQuestionsSelected
cellWithSlider.questionsToStudy = questionQuantityFromSlider
cellWithSlider.questionsChosenLabel.text = "Questions to study: \(questionQuantityFromSlider)"
} else { questionQuantityFromSlider = cellWithSlider.questionsToStudy
}
//Configure questions chosen label:
if questionsToStudyDict.isEmpty {
cellWithSlider.chooseSubjectsLabel.text = "Choose a subject"
cellWithSlider.questionsChosenLabel.text = "Questions to study: 0"
} else {
cellWithSlider.chooseSubjectsLabel.text = ""
}
return cellWithSlider
default:
return UITableViewCell()
}
case 2:
print("cellForRowAt case 2")
//Configure beginCell //
let cellWithStart = tableView.dequeueReusableCell(withIdentifier: "beginCell", for: indexPath) as! BeginStudyTableViewCell
//Curve corners
cellWithStart.startContainer.layer.cornerRadius = 2
cellWithStart.startContainer.layer.masksToBounds = true
return cellWithStart
default:
return UITableViewCell()
}
}
//Row heights
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
switch (indexPath.section) {
case 0:
return 120.0
case 1:
switch (indexPath.row) {
case 0:
return 60.0
case 1:
return 100.0
default:
return 44.0
}
case 2:
return 100.0
default:
return 44.0
}
}
override func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
if indexPath.section == 2 || indexPath.section == 0 {
return true
} else {
return false
}
}
override func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) {
if indexPath.section == 2 && selectedRowsDict.isEmpty != true && questionQuantityFromSlider > 0 {
let cellToBegin = tableView.cellForRow(at: indexPath) as! BeginStudyTableViewCell
cellToBegin.startContainer.backgroundColor = UIColor.lightGray
}
}
override func tableView(_ tableView: UITableView, didUnhighlightRowAt indexPath: IndexPath) {
if indexPath.section == 2 {
let cellToBegin = tableView.cellForRow(at: indexPath) as! BeginStudyTableViewCell
cellToBegin.startContainer.backgroundColor = UIColor.white
}
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch (indexPath.section) {
case 0:
//Set checkbox to ticked image
let cellWithSubject = tableView.cellForRow(at: indexPath) as! SubjectTableViewCell
cellWithSubject.subjectSelectedImageView.image = UIImage(named: "CheckboxTicked")
//Determine questions available for subject depending on unseen value
if showUnseenQuestions == true {
questionsToStudyDict[indexPath.row] = unseenQuestionsForSubjectArray[indexPath.row]
} else {
questionsToStudyDict[indexPath.row] = questionCountForSubjectArray[indexPath.row]
}
//Sum questions available
sumQuestionsSelected = Array(questionsToStudyDict.values).reduce(0, +)
//Reload table to pass this to questions available label in UISlider cell and reselect selected rows
let key: Int = indexPath.row
selectedRowsDict[key] = indexPath.row
self.tableView.reloadData()
if selectedRowsDict.isEmpty == false {
for (keys,_) in selectedRowsDict {
let index: IndexPath = NSIndexPath(row: selectedRowsDict[keys]!, section: 0) as IndexPath
tableView.selectRow(at: index, animated: false, scrollPosition: .none)
}
}
case 1:
break
case 2:
if selectedRowsDict.isEmpty != true && questionQuantityFromSlider > 0 {
self.performSegue(withIdentifier: "showStudyQuestion", sender: self)
} else {
print("Segue not fired")
}
default:
break
}
}
override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
if indexPath.section == 0 {
//Set checkbox to unticked image
let cellWithSubject = tableView.cellForRow(at: indexPath) as! SubjectTableViewCell
cellWithSubject.subjectSelectedImageView.image = UIImage(named: "Checkbox")
//Remove questions available for unselected subject from questions dictionary
questionsToStudyDict[indexPath.row] = nil
//Update sum of questions selected
sumQuestionsSelected = Array(questionsToStudyDict.values).reduce(0, +)
//Reload table to pass this to questions available label in UISlider cell and reselect selected rows
let key: Int = indexPath.row
selectedRowsDict[key] = nil
self.tableView.reloadData()
if selectedRowsDict.isEmpty == false {
for (keys,_) in selectedRowsDict {
let index: IndexPath = NSIndexPath(row: selectedRowsDict[keys]!, section: 0) as IndexPath
tableView.selectRow(at: index, animated: false, scrollPosition: .none)
}
}
}
}
func reloadView(_ notification: Notification) {
//Change bool value
showUnseenQuestions = !showUnseenQuestions
//For keys in dict, update values according to showUnseenQuestion value
if showUnseenQuestions == true {
for (key,_) in questionsToStudyDict {
questionsToStudyDict[key] = unseenQuestionsForSubjectArray[key]
}
} else {
for (key,_) in questionsToStudyDict {
questionsToStudyDict[key] = questionCountForSubjectArray[key]
}
}
//Re-run sum dict function
sumQuestionsSelected = Array(questionsToStudyDict.values).reduce(0, +)
//Finally reload the view and reselect selected rows
let selectedRowsIndexes = tableView.indexPathsForSelectedRows
self.tableView.reloadData()
if selectedRowsIndexes != nil {
for i in (selectedRowsIndexes)! {
tableView.selectRow(at: i, animated: false, scrollPosition: .none)
}
}
}
func updateQuantity(_ notification: Notification) {
//Reload the view and reselect selected rows
let selectedRowsIndexes = tableView.indexPathsForSelectedRows
self.tableView.reloadData()
if selectedRowsIndexes != nil {
for i in (selectedRowsIndexes)! {
tableView.selectRow(at: i, animated: false, scrollPosition: .none)
}
}
}
func showAlert(_ notification: Notification) {
let alertController = UIAlertController(title: "Reset Seen Questions", message: "Are you sure you want to reset all questions to unseen?", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { action in
// ...
}
alertController.addAction(cancelAction)
let OKAction = UIAlertAction(title: "Reset", style: .default, handler:{(action:UIAlertAction) -> Void in
QuestionManager.questionWorker.resetHasSeenValues()
self.reloadData()
print("reloadData fired")
})
alertController.addAction(OKAction)
self.present(alertController, animated: true, completion: nil)
}
func reloadData() {
DispatchQueue.main.async(execute: {
self.tableView.reloadData()
})
}
func countOfQuestionsAlreadySeen() -> [Int] {
var questionSeenYesArray: [Int] = []
if openWriteDatabase() {
let queryYes = "SELECT SUM(hasSeen) FROM UserData GROUP BY subjectID"
let querySeenYes: FMResultSet? = writeDatabase?.executeQuery(queryYes, withArgumentsIn: nil)
while (querySeenYes?.next())! {
if let questionSeenYes = (querySeenYes?.int(forColumnIndex: 0)) {
questionSeenYesArray.append(Int(questionSeenYes))
}
}
}
return questionSeenYesArray
}
func resetHasSeenValues() {
if openWriteDatabase() {
let resetHasSeenValues = "UPDATE UserData Set hasSeen = 0"
_ = writeDatabase?.executeUpdate(resetHasSeenValues, withArgumentsIn: nil)
}
}
答案 0 :(得分:0)
在主队列中添加self.reloadData()
。出口更改应始终保持在主线程内。
let OKAction = UIAlertAction(title: "Reset", style: .default, handler:{(action:UIAlertAction) -> Void in
QuestionManager.questionWorker.resetHasSeenValues()
DispatchQueue.main.async {
self.tableView.reloadData()
}
print("reloadData fired")
})
答案 1 :(得分:0)
更多调试显示,unseenQuestionsForSubjectArray
方法未正确填充cellForRowAt
。我解决了这个问题,这解决了reloadData()
问题。谢谢大家的帮助。