将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
结果的用法是:( SomeButton
是RunningTableViewCell
的子视图)
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:
删除它。 所以当你滚动单元格将被重用,但isBotton
和isTop
variables将被isTop
。
override func prepareForReuse() { self.isBottom = false self.isTop = false }
awakeFromNib
在视图及其子视图被分配和初始化之后被调用。
所以你的代码从awakeFromNib
在RunningTableViewCell
为每个单元func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
之前调用委托方法func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
。 这就是为什么isTop
和isBottom
在awakeFromNib
中nil
原因。
您可以在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)") }
祝你好运!
- 运行时遇到错误(域= LaunchServicesError,代码= 0)
- 在iOS应用程序购买突然停止工作(错误代码= 5002发生)
- 由于未捕获exception“NSInternalInconsistencyException”而终止应用程序,原因:'无法在软件包中加载NIB
- NSURLConnection / CFURLConnection HTTP加载失败(kCFStreamErrorDomainSSL,-9813)iOS
- 如何添加两个或更多的buttonannotationView:MKAnnotationView?
- GCD vs performSelectorInBackground / performSelectorOnMainThread
- embedded导航控制器
- closuresUIImagePickerController后错误的屏幕尺寸
- 核心数据:谓有序关系中的第一个元素