“[UILabel]”类型的值没有成员 'numberOfLines'

Value of type '[UILabel]' has no member 'numberOfLines'

我正在使用 Swift。

我的描述文字被截断了。我希望显示所有文本,但是当我添加 numberOfLines = 0 时出现错误。

"Value of type '[UILabel]' has no member 'numberOfLines'"

不太确定我做错了什么。

class SurveyResultsViewController: UITableViewController {

    @IBOutlet var lblSortedScores       : [UILabel]!
    @IBOutlet var sortedTitle           : [UILabel]!

    @IBOutlet var sortedDescription     : [UILabel]! {
        didSet {
            sortedDescription.numberOfLines = 0
        }
    }



    @IBOutlet weak var cellFinishButton : UITableViewCell!

    var survey: LikertSurvey?
    var points = [0, 0, 0, 0]
    var results: [(Int, Int)] = []


    var descriptionLabels =
        [("Money Avoiders think that money is bad and that rich people are greedy. They often feel like they don't deserve to have money.\n\nAvoiders may have a hard time sticking to a budget, can be compulsive buyers at times, and would rather not look at their bank statements."),
        ("Money Worshippers believe that money will make them happier and solve their problems, but they will never have enough of it.\n\nWorshippers are likely to overspend or have credit card debt. They also may overwork themselves at the expense of close relationships. "),
        ("People with the Money Status belief see money as a way of achieving higher status. They can believe their self worth is tied to their net worth.\n\nThose may lie about money, pretend to be richer than they are, and take on risks to make money quickly. "),
        ("Those with the Money Viligance belief believe in saving money. They can often be anxious and secretive about their finances.\n\nThey may be overly wary of their finances and scared to buy anything on credit.")]

    override func viewDidLoad() {
        super.viewDidLoad()

        UserDefaults.standard.setValue(points, forKey: "points")

        self.setResults()
        self.initUI()


    }



    private func setResults() {
        for (index, point) in points.enumerated() {
            results.append((index, point))
        }

        results.sort { (result1, result2) -> Bool in
            return result1.1 > result2.1
        }
    }

    private func initUI() {
        for i in 0 ..< results.count {
            let title = survey!.questionCategories[results[i].0]
            let description = descriptionLabels[results[i].0]
            lblSortedScores[i].text = "\(results[i].1) points"
            sortedTitle[i].text = "\(title)"
            sortedDescription[i].text = "\(description)"

        }

        let finishButtonTap = UITapGestureRecognizer(target: self, action: #selector(self.finishButtonTapped(_:)))
        cellFinishButton.addGestureRecognizer(finishButtonTap)

        self.navigationItem.hidesBackButton = false

    }

    @objc func finishButtonTapped(_ sender: UITapGestureRecognizer) {
        self.survey?.completed = true
        let alertController = UIAlertController(title: "Congratulations! You earned 100 XP from completing this quest!", message: "", preferredStyle: .alert)

                     alertController.addAction(UIAlertAction(title: "Ok",
                                                             style: .default) { action in
                         self.performSegue(withIdentifier: "unwindToSectionTableView", sender: self)
                     })

                     self.present(alertController, animated: true, completion: nil)

    }

}

如果是因为 UILabel 在括号中,我该如何解决这个问题?当我删除它们时,我只会收到更多错误。

变量 sortedDescription 的类型为 [UILabel] - UILabel 的数组。 UILabel class 有 属性 numberOfLines 而不是数组 class。

如果要为 sortedDescription 数组中的每个 UILabel 设置 numberOfLines,请尝试如下操作:

var sortedDescription: [UILabel]! {
      didSet {
        for label in sortedDescription {
          label.numberOfLines = 0
        }
      }
}

我复制了你的代码,运行,我探索了一些错误。第一

@IBOutlet var sortedDescription     : [UILabel]! {
        didSet {
            sortedDescription.numberOfLines = 0
        }
 }

UILabel 数组没有 numberOfLines。你应该改成这个

@IBOutlet var sortedDescription     : [UILabel]! {
        didSet {
            sortedDescription.forEach({ (label) in
                label.numberOfLines = 0
            })
        }
 }