将variables传递给UIViewController中的自定义UITableViewCell

这里是我如何使用UIViewController自定义UITableViewCell RunningTableViewCell

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! RunningTableViewCell //....... cell.isTop = false if(indexPath.row == 0){ cell.isTop = true } cell.isBottom = false if(indexPath.row == myArray.count-1){ cell.isBottom = true } return cell } 

这里是我的RunningTableViewCell类:(单元格的GUI是在故事板内)

 class RunningTableViewCell: UITableViewCell { //@IBOutlet ... @IBOutlet weak var myButton: SomeButton! var isTop: Bool? var isBottom: Bool? override func awakeFromNib() { super.awakeFromNib() print("result: \(self.isTop) \(self.isBottom)") myButton.isTop = self.isTop myButton.isBottom = self.isBottom } } 

它返回result: nil nil

结果的用法是:( SomeButtonRunningTableViewCell的子视图)

 class SomeButton: UIButton { var isTop = false var isBottom = false override func drawRect(rect: CGRect) { if(isTop){ // DO SOMETHING... } if(isBottom){ // DO SOMETHING... } } } 

那么如何将数据传递给RunningTableViewCell?

你需要重写单元格中的prepareForReuse 。 并从tableView:indexPath:删除它。 所以当你滚动单元格将被重用,但isBottonisTopvariables将被isTop

 override func prepareForReuse() { self.isBottom = false self.isTop = false } 

awakeFromNib在视图及其子视图被分配和初始化之后被调用。

所以你的代码从awakeFromNibRunningTableViewCell为每个单元func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell之前调用委托方法func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 。 这就是为什么isTopisBottomawakeFromNibnil原因。

您可以在RunningTableViewCell定义方法,该方法将使用此variables加载单元格。

 func load(isTop: Bool, isBottom: Bool) { self.isTop = isTop self.isBottom = isBottom // Update cell UI as you wish } 

最后在您的视图控制器中重写委托方法func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! RunningTableViewCell let isTop = indexPath.row == 0 let isBottom = indexPath.row == myArray.count-1 cell.load(isTop, isBottom: isBottom) return cell } 
 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! RunningTableViewCell //....... cell.isTop = false if(indexPath.row == 0){ cell.isTop = true cell.tag=100 } cell.isBottom = false if(indexPath.row == myArray.count-1){ cell.isBottom = true cell.tag=200 } return cell } 

也得到这个像…

 class RunningTableViewCell: UITableViewCell { //@IBOutlet ... var isTop: Bool? var isBottom: Bool? override func awakeFromNib() { super.awakeFromNib() if (self.tag==100) { isTop=true } else if (self.tag==200) { isBottom=true } else{ isTop=false isBottom=false } print("result: \(self.isTop) \(self.isBottom)") } } 

也使用单例方法…

您可以通过在您的单元格中调用自定义方法来解决此问题,

在UIViewController里面:

 //....... cell.isTop = false if(indexPath.row == 0){ cell.isTop = true } cell.isBottom = false if(indexPath.row == myArray.count-1){ cell.isBottom = true } cell.UpdateViews() return cell } 

在TableViewCell里面:

 //@IBOutlet ... var isTop: Bool? var isBottom: Bool? func updateViews() { print("result: \(self.isTop) \(self.isBottom)") } 

祝你好运!