我需要在表格视图单元格中将标签作为子视图添加到 UIStackView。
我创建了标签为
let nameLabel=UILabel()
nameLabel.text=names[indexPath.row]
其中 name 是一个数组,它是一种String
我的代码是
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource
{
let names=["Amutha","Priya","Amuthapriya","Priyasri","Kavisha"]
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return names.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->
UITableViewCell
{
let cell=tableView.dequeueReusableCell(withIdentifier: "cell") as! ViewCell
for name in names
{
let nameLabel=UILabel()
nameLabel.text=name
cell.nameStackView!.addSubview(nameLabel)
}
return cell
}
}
为什么当我向 stackview 添加标签时出现空指针异常?
任何帮助都感激不尽。