我有一个UIViewController
,里面有UITableView
。在该表中,我只有几行,每行都有几个按钮。当用户单击按钮时,按钮的图标将更改。我设法将按钮的状态保存在一个对象中,但是我遇到了一个错误,即当我检查所选按钮时会随机选择其他按钮。
这是我的代码:
// MODEL
class ChecklistItem: NSObject, NSCoding {
// Recorded values
var vehiclePass = 0 // 0 = unchecked, 1 = pass, 2 = fail
var trailerPass = 0 // 0 = unchecked, 1 = pass, 2 = fail
var vehicleComment = String()
var trailerComment = String()
}
// Set the cell of UITableView.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: Constants.checklistCell, for: indexPath) as! ChecklistCell
let itemCategory = itemSections[indexPath.section]
let item = itemCategory.checklistItems[indexPath.row]
titleForSelectedQuestion = item.descript
cell.delegate = self
cell.configCell(item)
// 0 = questions is not checked yet ( white icon ), 1 = question was checked as PASS ( green icon ), 2 = question was checked as FAIL ( red icon )
if item.vehiclePass == 0 {
// vehicle pass = white icon & vehicle fail = white icon
cell.vehiclePassButton.setImage(UIImage(named: Constants.whiteTickIcon), for: UIControl.State.normal)
cell.vehicleFailButton.setImage(UIImage(named: Constants.whiteCrossIcon), for: UIControl.State.normal)
}
if item.trailerPass == 0 {
// trailer pass = white icon & trailer fail = white icon
cell.trailerPassButton.setImage(UIImage(named: Constants.whiteTickIcon), for: UIControl.State.normal)
cell.trailerFailButton.setImage(UIImage(named: Constants.whiteCrossIcon), for: UIControl.State.normal)
}
if item.vehiclePass == 1 {
// vehicle pass = green icon & vehicle fail = white icon
cell.vehiclePassButton.setImage(UIImage(named: Constants.greenTickIcon), for: UIControl.State.normal)
cell.vehicleFailButton.setImage(UIImage(named: Constants.whiteCrossIcon), for: UIControl.State.normal)
}
if item.vehiclePass == 2 {
// vehicle pass = white icon & vehicle fail = red icon
cell.vehiclePassButton.setImage(UIImage(named: Constants.whiteTickIcon), for: UIControl.State.normal)
cell.vehicleFailButton.setImage(UIImage(named: Constants.redCrossIcon), for: UIControl.State.normal)
}
if item.trailerPass == 1 {
// trailer pass = green icon & trailer fail = white icon
cell.trailerPassButton.setImage(UIImage(named: Constants.greenTickIcon), for: UIControl.State.normal)
cell.trailerFailButton.setImage(UIImage(named: Constants.whiteCrossIcon), for: UIControl.State.normal)
}
if item.trailerPass == 2 {
// trailer pass = white icon & trailer fail = red icon
cell.trailerPassButton.setImage(UIImage(named: Constants.whiteTickIcon), for: UIControl.State.normal)
cell.trailerFailButton.setImage(UIImage(named: Constants.redCrossIcon), for: UIControl.State.normal)
}
print("For section \(indexPath.section) row: \(indexPath.row) - vehicle status = \(item.vehiclePass)")
print("For section \(indexPath.section) row: \(indexPath.row) - trailer status = \(item.trailerPass)")
return cell
}
}
extension ChecklistVC: ChecklistCellDelegate{
// Check if user pressed Pass or Fail btn for Vehicle/Trailer and scroll to next question
func tappedOnVehicleOrTrailerButtons(vehiclePassBtn: UIButton, vehicleFailBtn: UIButton, trailerPassBtn: UIButton, trailerFailBtn: UIButton, selectedCell: ChecklistCell) {
let indexPath = questionsTableView.indexPath(for: selectedCell)
let item = itemSections[indexPath?.section ?? 0].checklistItems[indexPath?.row ?? 0]
if vehiclePassBtn.isSelected {
item.vehiclePass = 1
}
if vehicleFailBtn.isSelected {
item.vehiclePass = 2
}
if trailerPassBtn.isSelected {
item.trailerPass = 1
}
if trailerFailBtn.isSelected {
item.trailerPass = 2
}
displayChecksLeft()
questionsTableView.reloadData()
print("For section \(indexPath?.section ?? 0) row: \(indexPath?.row ?? 0) - vehicle status = \(item.vehiclePass)")
print("For section \(indexPath?.section ?? 0) row: \(indexPath?.row ?? 0) - trailer status = \(item.trailerPass)")
if (vehiclePassBtn.isSelected || vehicleFailBtn.isSelected) && (trailerPassBtn.isSelected || trailerFailBtn.isSelected) {
displayChecksLeft()
scrollDown()
}
if (vehicleFailBtn.isTouchInside || trailerFailBtn.isTouchInside) {
self.lastIndexPath = indexPath
self.performSegue(withIdentifier: Constants.goChecklistAddComment, sender: nil)
}
}
}
这是单元格的代码:
protocol ChecklistCellDelegate {
func tappedOnVehicleOrTrailerButtons(vehiclePassBtn: UIButton, vehicleFailBtn: UIButton, trailerPassBtn: UIButton, trailerFailBtn: UIButton, selectedCell: ChecklistCell)
func tapGestureOnCell(_ selectedCell: ChecklistCell)
}
class ChecklistCell: UITableViewCell {
// Interface Links
@IBOutlet weak var questionName: UILabel!
@IBOutlet weak var vehiclePassButton: UIButton!
@IBOutlet weak var vehicleFailButton: UIButton!
@IBOutlet weak var trailerPassButton: UIButton!
@IBOutlet weak var trailerFailButton: UIButton!
@IBOutlet weak var vehicleCommentLabel: UILabel!
@IBOutlet weak var trailerCommentLabel: UILabel!
@IBOutlet weak var tagIconImageView: UIImageView!
@IBOutlet weak var tagNameLabel: UILabel!
@IBOutlet weak var defectImageView: CustomImageView!
// Constraints Links
@IBOutlet weak var vehicleCommentHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var trailerCommentHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var tagIconHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var tagNameBottomConstraint: NSLayoutConstraint!
@IBOutlet weak var defectImageHeightConstraint: NSLayoutConstraint!
// Properties
var delegate: ChecklistCellDelegate?
override func awakeFromNib() {
super.awakeFromNib()
let longTapGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.tapEdit(sender:)))
addGestureRecognizer(longTapGesture)
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
// Detect when the user press Long Tap on any cell
@objc func tapEdit(sender: UITapGestureRecognizer) {
delegate?.tapGestureOnCell(self)
}
// Config the cell for Defect and Damage Check
func configCell(_ checklistItem: ChecklistItem){
questionName.text = checklistItem.descript
defectImageView.image = UIImage(named: String(checklistItem.imagesPath?.last ?? String()))
vehicleCommentLabel.text = checklistItem.vehicleComment
trailerCommentLabel.text = checklistItem.trailerComment
tagIconImageView.image = UIImage(named: Constants.tagIcon)
}
// Function to change the icon when the user press Pass or Fail Btn
func changeIconOnBtnTap(checkedBtn: UIButton, uncheckedBtn: UIButton){
if checkedBtn.isSelected == true {
checkedBtn.isSelected = true // user can't uncheck the button
uncheckedBtn.isSelected = false // the unchecked button is white now
}else {
checkedBtn.isSelected = true
uncheckedBtn.isSelected = false
}
}
// Check if the user press Pass or Fail btn for Vehicle or Trailer
@IBAction func passOrFailBtnTapped(_ sender: UIButton) {
sender.isSelected = true
delegate?.tappedOnVehicleOrTrailerButtons(vehiclePassBtn: vehiclePassButton,
vehicleFailBtn: vehicleFailButton,
trailerPassBtn: trailerPassButton,
trailerFailBtn: trailerFailButton,
selectedCell: self)
}
}
以下是该错误的捕获:
感谢阅读。
答案 0 :(得分:1)
问题出在这里tappedOnVehicleOrTrailerButtons
if vehiclePassBtn.isSelected {
item.vehiclePass = 1
}
if vehicleFailBtn.isSelected {
item.vehiclePass = 2
}
if trailerPassBtn.isSelected {
item.trailerPass = 1
}
if trailerFailBtn.isSelected {
item.trailerPass = 2
}
您完全取决于按钮的isSelected
属性,该状态可以出队以更改模型值,但是您应该检查当前模型值并采取相应措施
自发送此参数, selectedCell: ChecklistCell
以来,请像其他方法一样使用
let indexPath = tableView.indexPath(for:cell)!
let itemCategory = itemSections[indexPath.section]
let item = itemCategory.checklistItems[indexPath.row]
// then check the value in item and go on
另一件事也不会在这里重新加载整个表
questionsTableView.reloadData()
但仅受影响的indexPath
questionsTableView.reloadRows([indexPath],for:.none)
并将其放在函数末尾
答案 1 :(得分:1)
如果有人要实现相同的功能,这里是解决方法:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.1/jquery.min.js"></script>
<ul class="answer-list" id="answer_list1">
<li>
<a tabindex="1" id="question1">
<span class="letter-option">A.</span>
<div>
<p>Plan risk management</p>
</div>
</a>
</li>
<li class="active">
<a tabindex="2" id="question1">
<span class="letter-option">B.</span>
<div>
<p>Identify risks</p>
</div>
</a>
</li>
<li>
<a tabindex="3" id="question1">
<span class="letter-option">C.</span>
<div>
<p>Plan risk responses</p>
</div>
</a>
</li><li>
<a tabindex="4" id="question1">
<span class="letter-option">D.</span>
<div>
<p>Perform qualitative risk analysis</p>
</div>
</a>
</li>
</ul>
感谢Sh_Khan试图帮助我!