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

问题描述 投票:0回答:1

我用的是Swift。

我的描述的文本被切断了,我想让所有的文本都显示出来,但是当我添加 numberOfLines = 0 时,我得到了一个错误。我希望所有的文本都能显示出来,但是当我添加 numberOfLines = 0 时,我得到一个错误。

"'[UILabel]'类型的值没有成员'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在括号里,我怎么才能解决这个问题?当我删除它们时,我只是得到更多的错误。

enter image description here

ios swift xcode iboutlet
1个回答
2
投票

变量"[UILabel]"的值没有... sortedDescription 属于 [UILabel] - 一系列 UILabel's. 的。UILabel 类的属性 numberOfLines 而不是 Array 类。

如果你想设置 numberOfLines 对于每个 UILabelsortedDescription 数组,试试这样的东西。

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

1
投票

我复制你的代码并运行,我发现了一些错误。首先

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

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

@IBOutlet var sortedDescription     : [UILabel]! {
        didSet {
            sortedDescription.forEach({ (label) in
                label.numberOfLines = 0
            })
        }
 }
© www.soinside.com 2019 - 2024. All rights reserved.