Ontop14 Ontop14 - 1 month ago 7
Swift Question

How to sort numbers in a textview in an UITableViewCell?

I have an UITextView in an UITableViewCell which displays a specific rating. How can I sort the UITableView in a way that the textviews are sorted in a from high to low order? This is the code for my textfield in case you need it:

let scoreText = UITextView()
scoreText.font = UIFont.systemFont(ofSize: 13.5)
scoreText.text = (String(userRating)) + " ✨"
scoreText.textColor = UIColor.gray
scoreText.textAlignment = NSTextAlignment.right
scoreText.translatesAutoresizingMaskIntoConstraints = false

Hope someone can help!

Answer Source

It's not a problem of UITextView. The order of your UITableView should be up to the order of the data source. For example,

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // In this case YOUR_DATA_LIST needs to be sorted beforehand.
    return YOUR_DATA_LIST.count