swift - 我无法将子视图添加到 UItableviewcell
问题描述
代码
import UIKit
class ViewController:
UIViewController,UITableViewDelegate,UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var suma=UITableViewCell()
var sampleviewpurple=UIView(frame: CGRect(x: suma.frame.width/0, y: 0, width: suma.frame.width/2, height: suma.frame.height))
sampleviewpurple.backgroundColor=#colorLiteral(red: 0.2196078449, green: 0.007843137719, blue: 0.8549019694, alpha: 1)
var sampleviewred=UIView(frame: CGRect(x: suma.frame.width/1, y: 0, width: suma.frame.width/2, height: suma.frame.height))
sampleviewred.backgroundColor=#colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1)
suma.contentView.addSubview(sampleviewpurple)
suma.contentView.addSubview(sampleviewred)
return suma
}
@IBOutlet weak var tab: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tab.dataSource=self
tab.delegate=self
}
}
当我尝试将子视图添加到 suma 并返回到委托时,我的表视图仍然是空白的,我正在将有效的数据源返回到数据源,并且还注意到对表视图的引用一切都很好,但是仍然如何我无法将子视图添加到 uitableviewcell
解决方案
在cellForRowAt
函数中,您必须将表格单元格出列,而不是实例化 UITableViewCell:
var suma = tableView.dequeueReusableCell(withIdentifier: "YOUR_CELL_IDENTIFIER")
推荐阅读
- android - 我的 Android Studio 中出现这些错误,我什至无法启动该项目
- azure-cosmosdb - 使用 NoSQL 数据库存储值列表的最佳实践
- javascript - 为什么我的神经网络训练方法没有被调用?(ML5.JS)
- node.js - 跨多核 Node 的 PM2 进程管理器
- c++ - 你能解释一下运行时错误是如何解决的吗
- c++ - 传递二维数组的问题
- python - 在 Pandas 中探索 GROUPBY 和 JOIN 的捷径,无需创建中间数据帧
- python - pytorch RuntimeError: 标量类型 Double 的预期对象,但得到标量类型 Float
- algorithm - 用我的程序找到模数为 10^9+7 的几何序列的总和
- terraform - 如何使用 Terraform 在 VmWare 中恢复快照?