这是我RunningTableViewCell
在内部使用自定义UITableViewCell的方法UIViewController
:
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
}
}
它返回 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
and和isTop
vars将被重置。
override func prepareForReuse() {
self.isBottom = false
self.isTop = false
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句