这是我在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
结果的用法是:(SomeButtonRunningTableViewCell内部的子视图)
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:中删除。因此,当您滚动时,该单元格将被重用,但isBottonisTop vars将被重置。

override func prepareForReuse() {
    self.isBottom = false
    self.isTop = false
}

10-08 06:25
查看更多