swift 3没有被调用的函数

时间:2017-10-08 09:51:32

标签: swift

当我点击addFavoriteButtonfav.tableview.insertRows函数之外的所有内容都被调用时,它不会崩溃或给我一个错误,但它没有被调用,因为它应该做的第一件事就是它被称为在控制台中打印一些东西。

import UIKit

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UISearchBarDelegate {



   // var favorites: favorites!
    var store: Store!

    var copiedarray = [String]()

    var wrds: manager_class = manager_class()
    var fav: favorites = favorites()
    //var historyObject: historyTableView = historyTableView()




    @IBOutlet var addFavoriteButton : UIButton!
    @IBOutlet var favorite: UIButton!


    @IBOutlet var TranslationLanguages: UIPickerView!
    @IBOutlet var translationLangTest: UILabel!

    @IBOutlet var SearchBar: UISearchBar!

    let arabic = ["مادي", "بسيط", "معقد", "سيارة"]
    let english = ["Concrete", "Simple", "Complex", "Car"]
    let french = ["Concret", "Simple", "Comlexe", "Car"]

    let languages = ["Pick a language","English to Arabic" , "English to French" , "Arabic to English" , "Arabic to French" , "French to English" , "French to Arabic"]




    @IBAction func addFavoriteButton(_ sender: Any) {

        print(SearchBar.text!)
        if (SearchBar.text?.isEmpty)! {
            return
        }
        for item in wrds.favorite_words {
            if item == SearchBar.text {
                print("item is already in favorite!")
                return
            }

        }

        wrds.favorite_words.append(SearchBar.text!)
        //print(wrds.favorite_words)
        let newItem = wrds.favorite_words.last


        if let index = wrds.favorite_words.index(of: newItem!) {

            let indexPath = IndexPath(row: index, section: 0)
            fav.tableView.beginUpdates()
            fav.tableView.insertRows(at: [indexPath], with: .automatic)
            fav.tableView.endUpdates()
        }

类收藏夹:UITableViewController {

var wrds1: manager_class = manager_class()

var store: Store!

//var tableCell = TableViewCell()
var copiedarray1 = [String]()
//let cell1: TableViewCell = TableViewCell()



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

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    print("i am inside table view!")
    let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
    print(wrds1.favorite_words.isEmpty)
    print("Heloo")


    let word = wrds1.favorite_words[indexPath.row]
    cell.favoriteWordInTable?.text = word


    return cell
}



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

    return copiedarray1.count
}
override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
    let title = "favorites"
    return title
}
override func viewDidLoad() {


    let wrds: ViewController = ViewController()
    //let wrds1: manager_class = manager_class()

    copiedarray1 = wrds.copiedarray



}

}

2 个答案:

答案 0 :(得分:1)

将鼠标光标指向要调用的函数的左侧,应该有一个圆点,如果您的按钮连接到此功能,当您单击该点时,您应该会看到弹出列表中显示的按钮。

您的问题应该是代码和故事板之间的连接,或者只是故事板上的连接。

答案 1 :(得分:0)

您在wrds.copiedarray中使用numberOfRowsInSection

wrds1.favorite_words中的cellForRow

我认为您必须像这样更改numberOfRowsInSection

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