Swift - 在 tableView 中选择单个 radioButton

标签 swift checkbox radio-button tableview sections

我有一个 tableView,它是可扩展的。 headerCell 有一个复选框、一个标签和一个单选按钮。 collapsableCell 有一个复选框和一个标签。

我使用了M13Checkbox 库来实现复选框和单选按钮。

问题是当我选择索引 0 处的单选按钮或 HeaderViewCell 的复选框时,索引 8,16,24 处的单选按钮/复选框也被选中。我知道这是因为 tableView 属性中的 numberOfSections,但是我如何一次只选择一个 radioButton。 我的要求是我必须在 tableView 中选择单个 radioButton,而 CheckBoxes 可以为 HeaderCell 选择多个。

我在这个问题上被严重困住了。我用谷歌搜索了很多但没有任何效果。非常感谢任何帮助或建议。

func numberOfSections(in tableView: UITableView) -> Int {
    return states.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return states[section].cities.count
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 50.0
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if (states[indexPath.section].expanded) {
        return 44
    }else{
        return 0.0
    }
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerCell = tableView.dequeueReusableHeaderFooterView(withIdentifier: "headerviewcell") as! HeaderView
    var list = states[section]
    headerCell.customInit(titleLabel: list.stateName, section: section, delegate: self)
    return headerCell
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "subcells") as! CollapsibleCell
    cell.selectionStyle = .none   
    cell.textLabel?.text = states[indexPath.section].cities[indexPath.row]
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            // works for headers or cell??
}

func toggleHeader(header : HeaderView, section : Int){
    states[section].expanded = !states[section].expanded

    tableView.beginUpdates()
    for i in 0 ..< states[section].cites.count {
        tableView.reloadRows(at: [IndexPath(row: i, section: section)], with: .automatic)
    }
    tableView.endUpdates()
}

更新: 这是我的 HeaderView 代码。每当我选择任何单选按钮时,其他部分的任何随机单选按钮也会被选中。滚动时,radioButtons 的状态和部分会发生变化。请帮我解决这个问题。我知道它与单元格重用属性有关,但我无法解决它。

import UIKit
import M13Checkbox

protocol HeaderViewDelegate {
    func toggleHeader(header : HeaderView, section : Int)
}

protocol CustomHeaderDelegate: class {
    func didTapButton(in section: Int, headerView : HeaderView, button : M13Checkbox)  
}

class HeaderView: UITableViewHeaderFooterView {

@IBOutlet weak var stateCheckBox: M13Checkbox!    
@IBOutlet weak var stateNameLabel: UILabel!
@IBOutlet weak var favouriteState: M13Checkbox!

var delegate : HeaderViewDelegate?
weak var delegateHeader: CustomHeaderDelegate?   
var sectionNumber : Int!
var section : Int!

override func awakeFromNib() {
    stateCheckBox.boxType = .square
    stateCheckBox = .bounce(.fill)

    favouriteState.boxType = .circle
    favouriteState.setMarkType(markType: .radio, animated: true)
    favouriteState.stateChangeAnimation = .bounce(.stroke)
}

override init(reuseIdentifier: String?) {
    super.init(reuseIdentifier: reuseIdentifier)
    self.addGestureRecognizer(UITapGestureRecognizer(target : self, action: #selector(selectHeaderView)))
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder : aDecoder)
    self.addGestureRecognizer(UITapGestureRecognizer(target : self, action: #selector(selectHeaderView)))
}

func selectHeaderView(gesture : UITapGestureRecognizer) {
    let cell = gesture.view as! HeaderView
    delegate?.toggleHeader(header: self, section: cell.section)
}

func customInit(titleLabel : String, section : Int, delegate : HeaderViewDelegate) {
    self.stateNameLabel.text = titleLabel
    self.section = section
    self.delegate = delegate
}

@IBAction func selectPrimaryCondition(_ sender: M13Checkbox) {
    // get section when favourite state radioButton is selected
    delegateHeader?.didTapButton(in: sectionNumber, headerView : self, button : sender)
}

 override func prepareForReuse() {
    // What do do here…??
} 
}

最佳答案

在不知道可以保存在某种对象类中的某种保存状态的情况下保存数据时,TableViews 很棘手,因为它可以做一些奇怪的事情,例如重用单元格;在您的情况下选择不正确的单元格。我猜你已经为你所在的州和城市创建了某种对象。我会确保它们是类而不是结构,并确保它们具有某种 isTapped Bool 这样您就可以像这样实际保存单个对象的状态:

class CellObject: NSObject {

    var isTapped: Bool

    init(isTapped: Bool) {
        self.isTapped = isTapped
    }
}

与其在 didSelect 方法中将 isSelected 设置为 true 或 false,不如将单元格对象 isTapped 设置为 true 或 false 以及将状态更改为任何你想要的想要,即将它的背景颜色更改为黑色以显示它已被选中。当在 cellForRowAtIndexPath 方法中加载单元格时,检查单元格对象 isTapped 是 true 还是 false 并相应地设置它的状态、颜色等。这是这个想法的准系统,但我希望这能为您指明正确的方向。

关于Swift - 在 tableView 中选择单个 radioButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49257519/

相关文章:

ios - 如何防止固定到底部的 HTML 元素在 iPhone 上的 WKWebView 中滚动

ios - destinationIndexPath 在 func collectionView(_ :dropSessionDidUpdate:withDestinationIndexPath:)

java - NatTable - CheckBoxCellEditor 和 CheckBoxPainter 在单击时显示文本

c# - 生成具有不同 'Content' 的单选按钮

excel - 如果标题没有文本,如何隐藏用户窗体上的选项按钮

ios - 禁用自动键盘关闭时崩溃

swift - didUpdateFocus()中区分UIButton

javascript - 遍历多个复选框并将选中的值添加到数组以写回 Sharepoint

c# - 如果Both Checkboxes flag false怎么办?

html - 如何居中单选按钮