2017-09-12 47 views
0

我創建了一個自定義單元的實現代碼如下。更新表格單元格變量的值之後

裏面我的UITableViewCell文件我有以下代碼:

var myTipView:EasyTipView? 

@IBAction func infoBtn_pressed(_ sender: UIButton) { 
    //print(diseases) 
    if self.myTipView == nil 
    { 
     self.myTipView = EasyTipView(text: diseases, preferences: EasyTipView.globalPreferences) 
     self.myTipView!.show(forView: sender) 

    } 
    else 
    { 
     self.myTipView!.dismiss() 
     self.myTipView = nil 
    } 
} 

和我的UIViewController裏面我與代碼實現代碼如下:

var myTipView:EasyTipView? 


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = myTable.dequeueReusableCell(withIdentifier: "diffCell") as! diffCell 

cell.myTipView = self.myTipView 

} 

的時候我試圖用自己的價值.myTipView我發現它零,這是顯而易見的,因爲直到infoBtn_pressed被激活,它沒有任何價值,在這種情況下cell.myTipView始終爲零當表第一次創建

我怎樣才能按下按鈕,一旦你設置myTipView值使用它裏面的UIViewController

回答

2

後self.myTipView有值,則需要通過執行此刷新表: -

tableView.reloadData() 
1

你可以嘗試添加一個觀察者您myTipView參數:

var myTipView : EasyTipView? { 
    didSet { 
     // Test that myTipView is non-nil 
     if let _ = myTipView { 
      // implement a function here that updates the cells in the table view... 
     } 
    } 
} 

我假設你可以處理更新的表細胞(順便說一句,您的實現似乎表明,每一個細胞都具有相同的屬性EasyTipView:是使y我們的意圖?)。希望有所幫助。

0

最後,我使用代表來通知表格,當單元格中的按鈕被點擊並且工作良好時(^^)