1

チェックボックス、ラベル、ラジオボタンを含むヘッダービューを備えた拡張テーブルビューがあります。

チェックボックスは複数選択できます。しかし、radioButton には single selection しかありません。別のラジオボタンが選択されている場合、以前に選択されていたボタンが tableView で選択解除されます

私の問題は、セクション 0 のラジオボタンを選択すると、セクション 8 と 16 のラジオボタンも選択されることです。ラジオボタンをスクロールすると、その状態が変わります。任意のセクションの任意のラジオ ボタンが選択されます。tableViewのセル再利用プロパティが原因であることは承知していますが、これを解決する方法がわかりません。ここSOで多くのソリューションを参照しましたが、どれも機能していないようです。この問題は私にとって非常に厄介で、これ以上先に進むことができません。間違っている場合、または何か不足している場合は、親切に案内してください。大変助かりました。ありがとうございました!

以下は、tableView の HeaderView のコードです。

import UIKit
import M13Checkbox

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

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

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!

var radioButtonSelected : Bool = false {
    didSet {
        if radioButtonSelected {
            favouriteState.checkState = .checked
        }else{
             favoriteState.checkState = .unchecked
        }
    }
}

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

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

ここに私の ViewController クラスがあります:

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

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

extension ViewController: HeaderDelegate {
        func didTapButton(in section: Int, headerView : HeaderView) {
         print("\(section)")
        }
}

期待される出力:

ここに画像の説明を入力

私が得ているもの:

ここに画像の説明を入力

4

0 に答える 0