ios - 从 UIAlertController 重新加载 tableView 数据不起作用

标签 ios swift uitableview

我在从 UIAlertController 刷新 tableView 数据时遇到问题。 该代码适用于测验式应用程序,此页面允许用户选择 主题以及一些其他选项(见屏幕截图)。有一个 “仅显示看不见的问题”旁边的重置按钮会触发 UIAlertController。但是,单击此警报中的重置操作 更新数据库但不更新 tableView。数据库是 肯定更新了,就好像我返回一个页面然后重新访问 tableView,更新主题单元格中看不见的问题值。我知道有很多 这里有这些类型的问题,但我担心通常的解决方法都不是 工作。

额外信息:

  • tableView 是用一系列定制的 UITableViewCells
  • 数据通过 FMDB 从 SQLite 数据库加载
  • UIAlertController 在重置时从 NSNotification 触发 按钮被点击

到目前为止我有:

  • 已检查数据源和委托(delegate)是否正确设置,以编程方式和 在IB中。通过 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()
    })
}

enter image description here

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)
    }
}

最佳答案

更多调试显示 unseenQuestionsForSubjectArray 没有在 cellForRowAt 方法中正确填充。我解决了这个问题,这解决了 reloadData() 问题。感谢大家的帮助。

关于ios - 从 UIAlertController 重新加载 tableView 数据不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42695416/

相关文章:

ios - AWS Amplify - 同步数据存储

ios - UITableView-删除部分中的最后一个单元格

ios - 同一 UITableView 中的不同单元格

ios - 如何使用 ViewController 中的不同对象在每台 iPhone 和 iPad 上全屏显示?

ios - 为什么 UICollectionView cellForItemat indexpath 会在一开始就为所有单元格调用

ios - 通过企业计划将应用程序发布到 Apple App Store

JSON 请求图像到 UIImageView

swift - 从闭包中获取值(value)并快速添加到 numberOfRowsInSection

ios - 连接到 LTE 时,应用程序无法访问互联网连接

ios - 不更新swift4中UItableViewCell的内容