swift 3没有被调用的函数

问题描述:

当我点击addFavoriteButton时,除fav.tableview.insertRows函数外都调用它,它不会崩溃或给我一个错误,但它不会被调用,因为它应该做的第一件事情所谓的是在控制台上打印一些东西。swift 3没有被调用的函数


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 



} 

}

+0

如果断点if条件里面,它不是停在那里,这意味着wrds.favorite_words.index(作者:!的newitem)是零。试着在控制台中输入“wrds.favorite_words.index(of:newItem!)”,并检查结果。 – MEnnabah

+0

我试着断点,它进入了if语句,它遍历所有的函数里面,insertRows不工作,它不插入新的行。 –

+0

什么是收藏夹? – MEnnabah

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

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

您在numberOfRowsInSection中使用wrds.copiedarray

and wrds1.favorite_words in cellForRow

我认为你必须改变numberOfRowsInSection这样的:

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