尝试将Bubblesort的每个迭代输出到标签文本以显示在屏幕上而不是控制台上。

每次更新数组时,我都尝试分配标签文本,但是它仅显示数组的最新版本,即已排序的数组

屏幕打印:



我要在标签上打印什么:



class ViewController: UIViewController {

    @IBOutlet weak var Label2: UILabel!

    public func bubbleSort<T> (_ arrays: [T], _ comparison: (T,T) -> Bool) -> [T]  {
        var array = arrays

        for i in 0..<array.count {
            for j in 1..<array.count-i {
                if comparison(array[j], array[j-1]) {
                    let tmp = array[j-1]
                    array[j-1] = array[j]
                    array[j] = tmp
                    print(array) // prints the array after each iteration to the console
                // inserting Label2.text = "\(array)" doesnt work as I intend it to.
                }
            }
        }


        return array
    }

    public func bubbleSort<T> (_ elements: [T]) -> [T] where T: Comparable {
        return bubbleSort(elements, <)
    }

}

最佳答案

这是使用tableView显示数据的好地方。


UITableView添加到ViewController。添加一个基本样式原型单元格,其中"cell"作为重用标识符。
将情节提要中的tableView附加到@IBOutlet var tableView: UITableView!
tableView.delegate中设置tableView.dataSourceviewDidLoad()
将属性var steps = [String]添加到ViewController
在bubbleSort的每个步骤中,将数组附加到stepssteps.append("\(array)")
numberOfRowsInSection()中,return steps.count
cellForRowAt()中,设置cell.textLabel?.text = steps[indexPath.row]
didSet使用steps属性观察器可以调用tableView.reloadData()




import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet var tableView: UITableView!

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

        cell.textLabel?.text = steps[indexPath.row]

        return cell
    }

    var steps = [String]() {
        didSet {
            tableView.reloadData()
        }
    }

    public func bubbleSort<T> (_ arrays: [T], _ comparison: @escaping (T,T) -> Bool) -> [T]  {
        var array = arrays

        for i in 0..<array.count {
            for j in 1..<array.count-i {
                if comparison(array[j], array[j-1]) {
                    let tmp = array[j-1]
                    array[j-1] = array[j]
                    array[j] = tmp
                    print(array) // prints the array after each iteration to the console
                    steps.append("\(array)")
                }
            }
        }

        return array
    }

    public func bubbleSort<T> (_ elements: [T]) -> [T] where T: Comparable {
        return bubbleSort(elements, <)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.dataSource = self
        tableView.delegate = self
    }

    @IBAction func go(button: UIButton) {
        steps = []
        _ = bubbleSort([33, 45, 25, 356, 5, 90, 14])
    }
}

关于ios - 将每次迭代打印到标签,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57438676/

10-12 00:11
查看更多