简体   繁体   English

如何在按下按钮时从数组数据中显示 iOS tableview - Swift

[英]How to display iOS tableview from array data on button press - Swift

I have an array that I am trying to use to populate a tableview on a button press (nav bar button).我有一个数组,我试图用它来在按下按钮(导航栏按钮)时填充表格视图。 I have used print statements to verify the proper data is present, but I am unable to get the table rows to display on the button press.我已使用打印语句来验证是否存在正确的数据,但我无法在按下按钮时显示表格行。

I assume the issue lies in my @IBAction func favoritesButton(_ sender: UIBarButtonItem) syntax, but now sure how.我认为问题在于我的@IBAction func favoritesButton(_ sender: UIBarButtonItem)语法,但现在确定如何。 What am I missing?我错过了什么?

class ViewController: UIViewController {

    @IBOutlet weak var searchBar: UISearchBar!

    @IBOutlet weak var tableView: UITableView!

    var materialData = ["One", "Two", "Three", "Four", "Five"]

    override func viewDidLoad() {
        super.viewDidLoad()

        print(favoritesData)

    }

    @IBAction func arrayList(_ sender: UIBarButtonItem) {
        print(favoritesData)
    }

    @IBAction func favoritesButton(_ sender: UIBarButtonItem) {
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
            cell.textLabel?.numberOfLines = 0
            cell.textLabel?.text = favoritesData[indexPath.row]
            print(favoritesData)
            return cell
        }
    }
}

var searchMaterial = [String]()
var searching = false
var favoritesData = [String]()

extension ViewController: UITableViewDelegate {

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        print(self.materialData[indexPath.row], "selected!")
    }

    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

        let favorite = UITableViewRowAction(style: .default, title: "Favorite") { (action, indexPath) in

            var data: String

            if searching {
                data = searchMaterial[indexPath.row]
                print(searchMaterial[indexPath.row], "added to favorites")
            } else {
                data = self.materialData[indexPath.row]
                print(self.materialData[indexPath.row], "added to favorites")
            }
            if let index = favoritesData.firstIndex(of: data) {
                favoritesData.remove(at: index)
            }
            else {
                favoritesData.append(data)
            }

        }
        favorite.backgroundColor = UIColor.orange
        return [favorite]
    }

}

extension ViewController: UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if searching {
            return searchMaterial.count
        } else {
            return materialData.count
        }
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.numberOfLines = 0
        if searching {
            cell.textLabel?.text = searchMaterial[indexPath.row]
        } else {
            cell.textLabel?.text = materialData[indexPath.row]
        }
        return cell
    }

}

extension ViewController: UISearchBarDelegate {

    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        searchBar.setShowsCancelButton(true, animated: true)
        searchMaterial = materialData.filter({$0.prefix(searchText.count) == searchText})
        searching = true
        tableView.reloadData()
    }

    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        searchBar.setShowsCancelButton(false, animated: true)
        searching = false
        searchBar.text = ""
        tableView.reloadData()
        tableView.endEditing(true)
    }

}


Dont write cellForRow in button action.. cellForRowAtIndexPath is a method that your dataSource provides to the UITableView .不要在按钮操作中编写cellForRow .. cellForRowAtIndexPath 是您的dataSource提供给UITableView的方法。 It is called by iOS to load a cell for a specific indexPath.它由 iOS 调用以加载特定 indexPath 的单元格。 It works by dequeueing a reusable cell and filling in the information for that cell.它通过使可dequeueing单元出列并填充该单元的信息来工作。

You typically do not call this.您通常不会调用它。 iOS does so iOS 这样做

In your button action just set your DataSource like in your case set favoritesData like favoritesData = data and then call tableView.reloadData()在您的按钮操作中,只需像在您的情况下设置您的 DataSource 一样设置favoritesData like favoritesData = data然后调用tableView.reloadData()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何快速显示来自数组数组的tableview单元格中的数据 - How to display the data in the tableview cell from the array of array in swift 快速按按钮显示来自阵列的问题 - Display questions from an array in order on a button press in swift 如何在Swift iOS的单个tableview单元格中显示多个字典或数组数据 - how to display mulitple dictionary or array data in single tableview cell in swift ios Firestore:如何从IOS(Swift)中的Firestore获取对象数据并在TableView中显示它们 - Firestore: How to get the object data from firestore in IOS (Swift) and display it in tableview Swift:在警报视图中按下按钮后,tableView不会重新加载数据 - Swift: The tableView do not reload data after press button in alert view 在按下按钮时显示tableView-iOS - Revealing a tableView on button press - iOS 如何使数组中的元素组合并快速显示在tableView上 - How to make the combination of elements from an array and display on tableView in swift 如何通过按一个按钮从一个数组中随机快速地引出一个简单的报价? - How to print by press of a button a simple quote in swift at random from an array? 如何在ios中的react-native-tableview中显示数组数据 - How to display array data in react-native-tableview in ios 如何在tableview swift ios中显示已保存的清单 - How to display saved checklist in tableview swift ios
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM