UITableView和UIButtons中的多个部分都很快

时间:2018-02-14 09:07:13

标签: ios swift uitableview uibutton

我有一个包含部分(Category0,Category1,..)的UITableView,并且特定部分的每一行都是一个UITableView,其中有一个部分是问题(Question1,..),行是可选的得到回答(option1,option2,..)。 问题是当我点击特定类别中的按钮和特定问题(Category0,question1,option0)时,请参见screenshot1, enter image description here

立即点击其他类别中的其他按钮(Category1,question2,option0),请参阅screenshot2,

enter image description here

和(Category4,question1,option0)见screenshot3。

enter image description here

以下代码:

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as?  insideTableViewCell

    cell?.answerlabel.text = "option \(indexPath.row)"


    cell?.initCellItem(id: (myObject?.id)! , answer: (myObject?.answerArray![indexPath.row] as? String)!)
    return cell!

}

在一个自定义的UITableViewCell里面,它位于TableViewCell:

     func initCellItem(id: Int , answer: String) {

        radioButton.setImage( imageLiteral(resourceName: "unchecked"), for: .normal)
        radioButton.setImage( imageLiteral(resourceName: "checked"), for: .selected)

        radioButton.tag = id
        radioButton.setTitle(answer, for: UIControlState.disabled)
        radioButton.addTarget(self, action: #selector(self.radioButtonTapped), for: .touchUpInside)
    }

  @objc func radioButtonTapped(_ radioButton: UIButton) {
        print(radioButton.tag)
        print(radioButton.title(for: UIControlState.disabled) as Any)
        let answer = radioButton.title(for: UIControlState.disabled) as Any
        let StrId = String(radioButton.tag)
        defaults.set(answer, forKey: StrId)
        let isSelected = !self.radioButton.isSelected
        self.radioButton.isSelected = isSelected
        if isSelected {
            deselectOtherButton()
        }
    }

  func deselectOtherButton() {
        let tableView = self.superview as! UITableView

        let tappedCellIndexPath = tableView.indexPath(for: self)!
        let section = tappedCellIndexPath.section
        let rowCounts = tableView.numberOfRows(inSection: section)

        for row in 0..<rowCounts {
            if row != tappedCellIndexPath.row {
                let cell = tableView.cellForRow(at: IndexPath(row: row, section: section)) as! insideTableViewCell
                cell.radioButton.isSelected = false
            }
        }
    }

3 个答案:

答案 0 :(得分:0)

您尚未发布仍在猜测的代码。

您可以创建像

这样的模型对象
class QuestionData {
    var strQuestion:String? // This may contains Question
    var strOptions:[String]? // It may contains options titles of your buttons
    var selectedAnswerIndex:Int? // When any button tapped 
}

你应该创建像

这样的类别模型
class Categories {
    var categoryTitle:String? 
    var questions:[QuestionData] = []
}

您可以使用此Categories类作为dataSource数组的主要来源

var arrayDataSource = [Categories]()

并填写原始数据。

现在无论何时点击任何按钮,您都可以使用selectedAnswerIndex:Int存储当前所选的问题选项。如果它为null,则用户尚未选择任何选项。

我创建了类,因此它是引用类型,您可以直接设置值而无需担心

希望它对你有所帮助

答案 1 :(得分:0)

我认为它有一些简单的代码可以帮助你: - 如果它不适合你,请不要介意: -

 if (!btnGreen3.isSelected)
    {
        btnGreen3.isSelected = !btnGreen3.isSelected
    }
    btnBlue3.isSelected = false
    btnBlack3.isSelected = false

答案 2 :(得分:-1)

您需要保存每个单元格的状态。

原因是您在滚动时使用dequereuseable cell with identifier切换到另一个单元格。

所以make array或Dictionary保存每个选定和未选定行的状态。