首页 > 解决方案 > 找不到 tableViewCell 不可编辑且未显示默认文本的原因

问题描述

抱歉这个无知的问题,但我正在尝试按照我学校的 appdev 教程进行操作,但我遇到了一个错误,我试图创建可编辑的表格视图单元格,但默认文本和编辑能力都没有出现。我签入了自定义 TableViewCell 类,TextView 插座就在那里。任何帮助将不胜感激:3

import UIKit

class AddVerbViewController: UIViewController, UITableViewDelegate, 
UITableViewDataSource{

var isPickerViewOpened : Bool = false

@IBOutlet weak var TableView: UITableView!

@IBAction func DoneButtonTapped(_ sender: Any) {
    dismiss(animated: true, completion: nil)
}

@IBAction func ClearButtonTapped(_ sender: Any) {
    dismiss(animated: true, completion: nil)
}
override func viewDidLoad() {
    super.viewDidLoad()

    TableView.delegate = self
    TableView.dataSource = self

    print("yes")
    // Do any additional setup after loading the view.
}
//MARK: - Table View Methods
func numberOfSections(in tableView: UITableView) -> Int {
    print("number of sections : 2")
    return 2

}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if (section == 0){
        return 2
    } else {
        return 2
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "AddVerbCell", for: indexPath)as! AddVerbTableViewCell
    cell.TextView.textContainer.maximumNumberOfLines = 1
    cell.TextView.textContainer.lineBreakMode = .byTruncatingTail
    cell.PickerView.isHidden = true

    if (indexPath.section == 0) {
        cell.TextView.isEditable = true
        cell.TextView.textColor = UIColor.gray
        cell.TextView.isScrollEnabled = false
        if (indexPath.row == 0){
            print("cell Name")
            cell.TextView.text = "Name"
        } else {
            cell.TextView.text = "TeForm"
        }
    } else {
        if (indexPath.row == 0) {
            cell.TextView.isEditable = false
            cell.TextView.isSelectable = false
            cell.TextView.textColor = UIColor.black
            cell.TextView.text = "PotentialForm"
        } else {
            cell.TextView.isHidden = true
            cell.PickerView.isHidden = false
        }
    }
    return cell
}

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 20
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if (indexPath.section == 0) {
        return 50
    } else {
        if (indexPath.row == 0){
            return 50
        } else {
           return 100
        }
    }
    }
/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destination.
    // Pass the selected object to the new view controller.
}
*/

}

标签: iosxcodeuitableviewuitextview

解决方案


我真的想通了。所以我需要添加cell.TextView.isUserInteractionEnabled = true到适当的区域。现在它起作用了!给大家添麻烦了抱歉


推荐阅读