0

我有一个包含两个部分的表格视图,我希望禁用第二部分单元格内的 textView 的用户交互(这些已完成,用户不应该能够编辑他们的文本)。下面来自 cellForRowAt 的代码工作正常,但是当我添加一个新单元格(它被添加到第 0 节的末尾,这最终导致 userInteraction 被禁用,我不知道为什么。重新加载 viewController (关闭页面并再次打开它)似乎解决了这个问题。

关于为什么会发生这种情况的任何想法或想法?提前致谢。

如果需要任何进一步的细节,我很乐意分享。

这是来自 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()
}

页面看起来像这样,我还附加了来自 cellForRow 控制台的打印 在此处输入图像描述在此处输入图像描述

4

1 回答 1

1

细胞被重复使用。您需要确保isUserInteractionEnabled为所有单元格显式设置为适当的值,以确保它们没有使用过时的值。

对于 section 的单元格0,您需要设置:

cell.label.isUserInteractionEnabled = true
于 2019-07-14T12:05:36.077 回答