【发布时间】:2018-03-02 06:37:26
【问题描述】:
我想在UITableViewCell 中设置一个 UITextView。
在 TableView 函数的某些方法中,我编写了以下代码,
但是它会导致以下错误:
由于未捕获的异常“NSGenericException”而终止应用程序, 原因:'无法使用锚激活约束 和 因为他们 没有共同的祖先。约束或其锚点是否引用 不同视图层次结构中的项目?这是非法的。'
这是我在 tableView 中的代码:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = newTableView.dequeueReusableCell(withIdentifier: "NewCell", for: indexPath) as! NewCellTableViewCell
let tableTextView = UITextView()
tableTextView.translatesAutoresizingMaskIntoConstraints = false
tableTextView.leadingAnchor.constraint(equalTo: cell.leadingAnchor).isActive = true
tableTextView.topAnchor.constraint(equalTo: cell.topAnchor).isActive = true
tableTextView.widthAnchor.constraint(equalTo: cell.widthAnchor).isActive = true
tableTextView.heightAnchor.constraint(equalTo: cell.heightAnchor).isActive = true
cell.addSubview(tableTextView)
return cell
}
【问题讨论】: