我想在里面设置一个UITextView UITableViewCell
。在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
}
首先,您应该致电:
cell.addSubview(tableTextView)
然后附加约束,此外:
所有约束都必须仅涉及接收视图范围内的视图。具体来说,涉及的任何视图都必须是接收视图本身,或者是接收视图的子视图。
因此,您的代码可能是:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = newTableView.dequeueReusableCell(withIdentifier: "NewCell", for: indexPath) as! NewCellTableViewCell
let tableTextView = UITextView()
cell.addSubView(tableTextView)
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
return cell
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句