2017-10-08 93 views
0

當我點擊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

回答

1

點您的鼠標光標在你的函數的左側被調用,應該有一個圓點,如果您按鈕連接到此功能,當您單擊該點時,您應該看到彈出列表中顯示的按鈕。

你的問題應該是代碼和故事板之間的連接,或者只在故事板上。

0

您在numberOfRowsInSection中使用wrds.copiedarray

and wrds1.favorite_words in cellForRow

我認爲你必須改變numberOfRowsInSection這樣的:

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