【问题标题】:how to update data in tableview (Swift)如何更新表格视图中的数据(Swift)
【发布时间】:2016-09-05 22:04:53
【问题描述】:

我有两个 Swift 文件:NotificationsViewController 和 ViewController。 当点击按钮时,ViewContoller 中的 firstArray 会更新。我能够打印更新的数据。但是,当我切换回 NotificationsViewController 时,当我拉动刷新时,tableview 不会更新。

NotificationsViewController:

import Foundation
import UIKit

var  firstArray : [String] = ["123","1234"]

class NotificationsViewController: UITableViewController {


    var refresher: UIRefreshControl!

    var data: [[String]] = [firstArray, ["4","5"]]

    func refresh(){

        print("refreshed")
        self.tableView.reloadData()
        self.refresher.endRefreshing()

    }

    override func viewDidLoad() {


        refresher = UIRefreshControl()
        refresher.attributedTitle = NSAttributedString(string: "pull to refresh")

        refresher.addTarget(self, action: #selector(NotificationsViewController.refresh), forControlEvents: UIControlEvents.ValueChanged)
        self.tableView.addSubview(refresher)
        refresh()


        super.viewDidLoad()
        self.tableView.editing = true

    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
      return data.count

    }
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {



        return data[section].count

    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell: UITableViewCell = UITableViewCell(style: .Subtitle, reuseIdentifier: "tableCell")

        cell.textLabel?.text = data[indexPath.section][indexPath.row]

        return cell
    }

    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {

            data[indexPath.section].removeAtIndex(indexPath.row)
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)



        }
    }

}

视图控制器:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func button(sender: AnyObject) {
              firstArray.append("522")
              print(firstArray)

    }


}

我试过this,但也没用。

更新:

如何更新基于另一个数组的 cell.detailTextLabel?.text 的值?

提前谢谢你

【问题讨论】:

  • 更新/编辑现有项目的编辑风格是什么???

标签: ios iphone swift uitableview tableview


【解决方案1】:

一个问题:

更新第一个数组后。您还需要更新数据。它不会自动更新。所以你的代码看起来像

func refresh(){
    data = [firstArray, ["4","5"]]
    print("refreshed")
    self.tableView.reloadData()
    self.refresher.endRefreshing()
}

反馈:

与其在类之外使用变量,不如使用单例类。它看起来像这样:

class Singleton: NSObject {
    static let sharedInstance = Singleton()
    var firstArray = []
}

您可以像这样更新/检索数组

Singleton.sharedInstance.firstArray

【讨论】:

    猜你喜欢
    • 2021-04-12
    • 2021-08-22
    • 2020-04-16
    • 1970-01-01
    • 1970-01-01
    • 2021-05-26
    • 2018-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多