这是我在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
和isTop
vars将被重置。
override func prepareForReuse() {
self.isBottom = false
self.isTop = false
}