2017-09-12 35 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

最后,我使用代表来通知表格,当单元格中的按钮被点击并且工作良好时(^^)