如何对TableView A-Z进行排序? (斯威夫特3)

时间:2017-04-09 10:32:57

标签: swift swift3 tableview

enter image description here enter image description here

尚未编辑:

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    let arr = ["A","B","C","D","E","F","G","H","I","J","K","L","M"]

    @IBOutlet weak var tableView: UITableView!



//----------------------------------------------------------------------------//

    override func viewDidLoad() {
        super.viewDidLoad()



    }

//----------------------------------------------------------------------------//

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! Cell


        cell.thisIsMyText.text = arr[indexPath.row]

        return (cell)
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arr.count
    }
}

1 个答案:

答案 0 :(得分:3)

var sortedArray: [String] = []


...

sortedArray = arr.sorted()

cell.thisIsMyText.text = sortedArray[indexPath.row]