0

我需要在表格视图单元格中将标签作为子视图添加到 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 添加标签时出现空指针异常?

任何帮助都感激不尽。

4

5 回答 5

0

ViewCell尝试在您的类中初始化堆栈视图和标签。

class ViewCell: UITableViewCell {
    let nameLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 14)
        return label
    }()
    let nameStackView: UIStackView = {
        let stackView = UIStackView()
        stackView.axis = .vertical
        return stackView
    }()
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super .init(style: style, reuseIdentifier: reuseIdentifier)
        self.configureStackView()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func configureStackView()
    {
        addSubview(nameStackView)
        nameStackView.translatesAutoresizingMaskIntoConstraints = false
        nameStackView.topAnchor.constraint(equalTo: topAnchor).isActive = true
        nameStackView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
        nameStackView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
        nameStackView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true

        nameStackView.addArrangedSubview(nameLabel)
        nameLabel.translatesAutoresizingMaskIntoConstraints = false
        nameLabel.topAnchor.constraint(equalTo: nameStackView.topAnchor, 
        constant:10).isActive = true
        nameLabel.leadingAnchor.constraint(equalTo: nameStackView.leadingAnchor, 
        constant:10).isActive = true
        nameLabel.trailingAnchor.constraint(equalTo: nameStackView.trailingAnchor, 
        constant:-10).isActive = true
        nameLabel.bottomAnchor.constraint(equalTo: nameStackView.bottomAnchor, 
        constant:-10).isActive = true
    }
}

此代码是在您没有stackview在情节提要中创建的情况下使用的。

现在在您的类中,在您的方法ViewController中添加以下代码。cellForRowAt

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->
                UITableViewCell
{
    let cell=tableView.dequeueReusableCell(withIdentifier: "cell") as! ViewCell
    cell.nameLabel.text = names[indexPath.row]
    return cell
}

希望,这个解决方案适合你。

于 2020-01-29T09:32:21.563 回答
0

如果您有nameStackView故事板或 xib,请确保IBOutlet已正确连接。如果您以nameStackView编程方式创建了它,请确保它已初始化。

并从堆栈视图中删除所有现有标签,否则每个滚动条上都会有重复的标签

class ViewCell: UITableViewCell {
    //Make sure IBOutlet is connected properly
    @IBOutlet weak var nameStackView: UIStackView!
}

视图控制器

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
{
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! ViewCell
     for name in names
     {
        cell.nameStackView.arrangedSubviews.forEach { cell.nameStackView.removeArrangedSubview($0) }
        let nameLabel=UILabel()
        nameLabel.text=name
        cell.nameStackView.addSubview(nameLabel)

     }
     return cell
 }
于 2020-01-29T06:52:49.813 回答
0

您可以根据需要使用以下代码添加 UIStackView。

let titleLabel = UILabel()
let subtitleLabel = UILabel()

lazy var titleStackView: UIStackView = {

    titleLabel.textAlignment = .center
    titleLabel.text = "Good Morning"
    titleLabel.textColor = UIColor.white
    titleLabel.font = UIFont(name: "ProximaNova-Regular", size: 12.0)

    subtitleLabel.textAlignment = .center
    subtitleLabel.text = "--"
    subtitleLabel.textColor = UIColor.white
    subtitleLabel.heightAnchor.constraint(equalToConstant: 30.0).isActive = true
    subtitleLabel.font = UIFont(name: "DublinSpurs", size: 20.0)

    let stackView = UIStackView(arrangedSubviews: [subtitleLabel])
    stackView.axis = .vertical/.horizontal
    return stackView
}()

也尝试更换

for name in names
{
    let nameLabel=UILabel()
    nameLabel.text=name
    cell.nameStackView!.addSubview(nameLabel)
}

let nameLabel=UILabel()
nameLabel.text = names[indexPath.row]
cell.nameStackView!.addSubview(nameLabel)
于 2020-01-29T05:25:36.913 回答
0

试试下面的代码

let nameLabel = UILabel()

cell.nameStackView.addArrangedSubview(nameLabel)
于 2020-01-29T05:57:50.553 回答
0

改变

cell.nameStackView!.addSubview(nameLabel)

cell.nameStackView!.addArrangedSubview(nameLabel)
于 2020-01-29T05:03:12.547 回答