我有一个tableView,它是可扩展的。headerCell有一个checkBox、一个标签和一个radioButton。collapsableCell有一个checkBox和一个标签。
我使用M13Checkbox库来实现checkBox和radioButton。
问题是,当我在索引0处选择radioButton或checkBox of HeaderViewCell时,也会选择索引8、16、24处的radioButton/复选框。我知道这是因为numberOfSections in 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…??
}
}发布于 2018-03-13 14:37:34
当保存数据而不知道某种类型的保存状态(可以保存在某种对象类中)时,TableViews是很棘手的,它可以做一些奇怪的事情,例如重用单元;在您的情况下选择不正确的单元格。我猜你已经为你的州和城市创造了某种物体。我会确保它们是类而不是结构体,并确保它们具有某种isTapped Bool,这样您就能够为这样的单个对象实际保存状态:
class CellObject: NSObject {
var isTapped: Bool
init(isTapped: Bool) {
self.isTapped = isTapped
}
}不要只是在isSelected方法中将您的didSelect设置为true或false,而是将单元格对象isTapped设置为true或false,并将状态更改为任何您想要的状态,即将其背景颜色更改为黑色以显示它已被选中。在cellForRowAtIndexPath方法中加载单元格时,检查单元格对象isTapped是否为false,并相应地设置它的状态、颜色等。这是基本的想法,但我希望这指向正确的方向。
https://stackoverflow.com/questions/49257519
复制相似问题