我有一个包含两个部分的表格视图,我希望第二部分的单元格内的textView的用户交互功能被禁用(这些操作已完成,用户无意于编辑其文本)。而且cellForRowAt下面的代码可以正常工作,但是当我添加一个新的单元格(将其添加到第0节的末尾时,这种方式最终会导致禁用userInteraction,并且我不知道为什么。)页并再次打开它)似乎可以解决问题。
关于为什么会发生这种情况的任何想法或想法? 预先感谢。
如果需要更多详细信息,我很乐意与他们分享。
这是来自cellForRow的代码
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ActionCell", for: indexPath) as! StepCell
if indexPath.section == 0 {
cell.label.alpha = 1
// This code is needed to remove the strikethrough from this cell
let attr = NSMutableAttributedString(attributedString: (cell.label.attributedText)!)
let originalRange = NSMakeRange(0, attr.length)
attr.setAttributes([:], range: originalRange)
cell.label.attributedText = attr
cell.label.attributedText = NSMutableAttributedString(string: "", attributes: [:])
cell.label.text = goals[selectedCell].steps![indexPath.row].title
cell.checkbox.image = UIImage(named: "unticked")
cell.focus.isHidden = false
} else {
cell.label.alpha = 0.5
cell.label.attributedText = goals[selectedCell].completedSteps![indexPath.row].title.withStrikethroughStyle(.single)
cell.checkbox.image = UIImage(named: "ticked")
cell.focus.isHidden = true
cell.label.isUserInteractionEnabled = false
}
cell.label.textContainerInset = UIEdgeInsets(top: 20, left: -6, bottom: 20, right: 0);
cell.label.textColor = .white
cell.label.tintColor = UIColor(named: "Yellow")
cell.label.keyboardAppearance = .dark
cell.label.font = UIFont.boldSystemFont(ofSize: 18)
cell.label.disableKeyboardAccessory()
cell.label.delegate = self
cell.delegate = self
print("For \(indexPath) user intercation enabled is \(cell.label.isUserInteractionEnabled)")
return cell
}
这是添加新单元格的操作代码
@IBAction func plusTU(_ sender: Any) {
goals[selectedCell].steps?.append(Step(title: ""))
let numberOfCells = tableview.numberOfRows(inSection: 0)
tableview.reloadData()
tableview.layoutIfNeeded()
DispatchQueue.main.asyncAfter(deadline: .now()+0.2) {
let cell = self.tableview.cellForRow(at: IndexPath.init(row: numberOfCells, section: 0)) as? StepCell
cell?.label.becomeFirstResponder()
}
let indexPath = IndexPath(row: goals[selectedCell].steps!.count - 1, section: 0)
tableview.scrollToRow(at: indexPath, at: .bottom, animated: true)
progressBar.configure(goalsIndex: selectedCell)
configureCompleteGoalButton(buttonHeight: completeGoalButtonHeight, progressBarHeight: progressBarHeight, progressBar: progressBar)
}
如果相关,我有一个textView委托,看起来像这样:
extension GoalDetails: UITextViewDelegate {
func textViewDidChange(_ textView: UITextView) {
let cell = textView.superview?.superview as? StepCell
let indexPath = tableview.indexPath(for: cell!)
goals[selectedCell].steps![indexPath!.row].title = textView.text
resizeTableViewCell()
}
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if text == "\n" || text == "\t" {
textView.resignFirstResponder()
return false
}
return true
}
func textViewDidEndEditing(_ textView: UITextView) {
writeJSONGoals()
}
func textViewDidBeginEditing(_ textView: UITextView) {
if descriptionExists == true {
descriptionHeight.isActive = true
descriptionExpanded = false
}
}
}
它要调用的函数是这样:
func resizeTableViewCell(){
tableview.beginUpdates()
tableview.endUpdates()
tableview.layoutIfNeeded()
}
答案 0 :(得分:1)
单元格被重用。您需要确保将 /search
的所有单元格显式设置为适当的值,以确保它们未使用过时的值。
对于isUserInteractionEnabled
部分的单元格,您需要设置:
0