以下是我如何在UIViewController
中使用我的自定义单元格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在Storyboard中创建)
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?
awakeFromNib
在您设置cellForRowAtIndexPath
中的值之前被调用。 - pbasdfisTop
和isBottom
变量上使用自定义 setter,在这些值改变时立即更新 UI。 - pbasdf