【问题标题】:How can I reload the following viewcontroller with a tableview?如何使用 tableview 重新加载以下视图控制器?
【发布时间】:2019-09-09 01:21:46
【问题描述】:

我在视图控制器中有一个表格视图。当按下导航栏中的按钮时,我希望重新加载表格视图。

视图控制器叫FirstViewController,Tableview叫listTableView,刷新按钮叫refreshButton

在下面的代码中,我已经链接了 refreshButton 动作,但似乎无法确定在其中放入哪个函数以在按下时触发刷新。

以下是我的代码:

import UIKit

class FirstViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, FeedModelProtocol  {
    var feedItems: NSArray = NSArray()
    var selectedStock : StockModel = StockModel()
    let tableView = UITableView()
    @IBOutlet weak var listTableView: UITableView!
    @IBOutlet weak var refreshButton: UIBarButtonItem!

    override func viewDidLoad() {
        super.viewDidLoad()
        //set delegates and initialize FeedModel


        self.listTableView.delegate = self
        self.listTableView.dataSource = self

        let feedModel = FeedModel()
        feedModel.delegate = self
        feedModel.downloadItems()

    }

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }

    @IBAction func reloadData(_ sender: Any) {


    }



    func itemsDownloaded(items: NSArray) {

        feedItems = items
        self.listTableView.reloadData()
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // Return the number of feed items
        return feedItems.count

    }

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

        // Retrieve cell
        let cellIdentifier: String = "stockCell"
        let myCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
        myCell.textLabel?.textAlignment = .center
        // Get the stock to be shown
        let item: StockModel = feedItems[indexPath.row] as! StockModel
        // Configure our cell title made up of name and price


        let titleStr = [item.customer].compactMap { $0 }.joined(separator: "-")


        print(titleStr)
        // Get references to labels of cell
        myCell.textLabel!.text = titleStr

        return myCell
    }

}

【问题讨论】:

  • 你解决了吗?您只需要在导航按钮触发时调用 listTableView.reloadData()...
  • 不,这不起作用。
  • @RelsonJames,您的 refresh button 扇区 IBAction 操作在哪里,确实与您的 storyboard 导航栏的 IBAction 事件触发刷新按钮有关。我正在和这个@IBOutlet weak var refreshButton: UIBarButtonItem!
  • @RelsonJames 当您点击按钮时,IBAction 是否实际运行? (使用打印语句或调试器进行验证。)
  • @PhillipMills 是的,打印语句确实有效

标签: ios swift uitableview


【解决方案1】:

为此使用yourUITableView.reloadData()。更多信息请查看Apple's Developer page

【讨论】:

  • 不,由于某种原因这不起作用。代码现在说:@IBAction func reloadData(_ sender: Any) { listTableView.reloadData() }
  • 再看一遍,@IBAction func reloadData(_:)里面什么都没有。
猜你喜欢
  • 1970-01-01
  • 2015-09-10
  • 1970-01-01
  • 2011-02-03
  • 1970-01-01
  • 2015-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多