以下是我在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类:( Cell的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 } }
它返回结果: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 vars将被重置.
override func prepareForReuse() { self.isBottom = false self.isTop = false }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。