У меня есть tableView, который можно расширить. HeaderCell имеет checkBox, метку и radioButton. CollapsableCell имеет флажок и метку.
Я использовал библиотеку M13Checkbox для реализации checkBox и radioButton.
Проблема в том, что когда я выбираю RadioButton или checkBox HeaderViewCell с индексом 0, тогда также выбирается radioButton / checkBox с индексом 8,16,24. Я знаю, что это из-за свойства numberOfSections в свойстве tableView, но тогда как мне выбрать только один radioButton за раз. Мое требование: я должен выбрать один RadioButton в tableView, а 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. Всякий раз, когда я выбираю какой-либо radioButton, также выбирается любой случайный radioButton для других разделов. При прокрутке меняется состояние и раздел 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…??
}
}
cellForRowAt indexPath
реализует мои расширяемые / складывающиеся ячейки. как мне проверить условие для HeaderView, так как я должен выбирать только один radioButton за раз. - person Bella   schedule 13.03.2018viewForHeaderInSection
, но все равно безрезультатно. Он по-прежнему выбирает несколько переключателей, а также выбираются переключатели в других разделах. Я действительно не понимаю этого. - person Bella   schedule 13.03.2018tableView:didSelectRowAt:
вы сначала назначаетеself.selectIndexPath = indexPath
, а затем проверяетеif selectIndexPath.row == indexPath.row
- но так должно быть всегда, потому что вы просто присвоили ему одно и то же значение. - person Andreas Oetjen   schedule 13.03.2018