Ios 如何提供';实时';在表视图Xcode中更新

Ios 如何提供';实时';在表视图Xcode中更新,ios,swift,tableview,Ios,Swift,Tableview,我已经创建了一个显示玩家(蜜蜂)及其属性的Tableview。你也可以升级玩家,但是这个升级不是实时显示的。我必须在TableView上一直向下滚动,然后向上滚动以查看更新的值。有人知道怎么解决吗 import UIKit class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { var beeslevel: [Int] { return [beelevel,

我已经创建了一个显示玩家(蜜蜂)及其属性的Tableview。你也可以升级玩家,但是这个升级不是实时显示的。我必须在TableView上一直向下滚动,然后向上滚动以查看更新的值。有人知道怎么解决吗

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    var beeslevel: [Int] {
        return [beelevel, wasplevel, bumblebeelevel]
    }
    var beelevel = 0
    var wasplevel = 0
    var bumblebeelevel = 0
    let bees = ["bee", "wasp", "bumblebee"]

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

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

        cell.beeimage.image = UIImage(named: bees[indexPath.row] + ".png")
        cell.beelabel.text = bees[indexPath.row]
        cell.beelevel.text = "lvl \(beeslevel[indexPath.row])"

        return(cell)
    }

    @IBAction func beelevelup(_ sender: Any) {
        beelevelup()
    }

    func beelevelup() {
        beelevel += 1
        score -= 50
    }

如果我答对了这个问题,您实际上希望一行包含玩家信息的TableView在升级时被更新

您需要通过编写以下代码来重新加载单元格:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    var players:[String]?
    var tableView: UITableView?

    override func viewDidLoad() {
        super.viewDidLoad()

        players = ["Ronaldo", "Ronaldinho", "Messi", "Suarez", "Neymar", "Pogba", "Pele", "Rooney", "Ozil"]

        tableView = UITableView()
        tableView?.frame = CGRect.init(x: 10, y: 20, width: 200, height: 500)
        tableView?.dataSource = self
        tableView?.delegate  = self
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        view.addSubview(tableView!)

        let button = UIButton.init(type: UIButtonType.system)
        button.setTitle("Update Data", for: UIControlState.normal)
        button.frame = CGRect.init(x: 10, y: tableView!.frame.origin.y + tableView!.frame.size.height+20, width: tableView!.frame.width, height: 50)
        button.addTarget(self, action: #selector(onRowUpdate), for: UIControlEvents.touchUpInside)
button.backgroundColor = UIColor.brown
        view.addSubview(button)

    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

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

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = players?[indexPath.row]
        return cell
    }

    func onRowUpdate() {
        let index = NSIndexPath.init(item: 2, section: 0)
        players?[index.row] = "Traxido"
        tableView!.reloadRows(at: [index as IndexPath], with: UITableViewRowAnimation.automatic)
    }

}

您需要用新数组数据重新加载表。我将使用什么命令/代码?或者我该怎么做呢?问题是Swift,而不是Objective-C。我理解,但这只是为了提供信息,但我仍然会更新它。当您使用单个调用来更新表时,不需要调用
begin/endUpdates
。Apple开发者API参考:beginUpdate()和endUpdates()这些方法对可以嵌套。如果不在此块内进行插入、删除和选择调用,表属性(如行计数)可能会无效。我向您保证,在这种情况下不需要这些调用。试试看。只有在多次调用
insert/delete/move/reloadRows
并且需要它们的索引路径匹配时,才需要它们。