以下是我在UIViewController中使用自定义UITableViewCell RunningTableViewCell的方法: func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) - UITableViewCell { let cell = tableView.dequeueReusableC
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
}
