【问题标题】:How to make a UITableViewCell that inherits from another UITableViewCell?如何制作一个继承自另一个 UITableViewCell 的 UITableViewCell?
【发布时间】:2021-05-12 16:32:28
【问题描述】:

我创建了一个 UITableViewCell,它基本上是一张背后有阴影的卡片。我想让它成为用于所有其他自定义 UITableView 控制器的基类。但是,当我使用下面的代码时,阴影卡没有出现。这是为什么呢?

ShadowCardTableViewCell

class ShadowCardTableViewCell: UITableViewCell {

    let borderView = UIView(frame: .zero)
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        changeToShadowedCard()
        self.contentView.layoutIfNeeded()
     }

     required init?(coder aDecoder: NSCoder) {
       super.init(coder: aDecoder)
    }
    
    func changeToShadowedCard() {
        backgroundColor = .clear
        contentView.addSubview(borderView)
        borderView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            borderView.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: 10),
            borderView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: -10),
            borderView.rightAnchor.constraint(equalTo: self.contentView.rightAnchor, constant: -10),
            borderView.leftAnchor.constraint(equalTo: self.contentView.leftAnchor, constant: 10),
        ])
        borderView.backgroundColor = .secondarySystemBackground
        borderView.layer.cornerRadius = 7.5
        borderView.addShadow(shadowColor: UIColor.label.cgColor, shadowOffset: CGSize(width: 0, height: 0), shadowOpacity: 0.3, shadowRadius: 4)
    }
}

OtherTableViewCell

class OtherTableViewCell: ShadowCardTableViewCell {

    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
     }

     required init?(coder aDecoder: NSCoder) {
       super.init(coder: aDecoder)
    }
}

【问题讨论】:

  • 你的addShadow(...) func 在做什么?
  • 而且...您是否通过tableView.register(OtherTableViewCell.self, forCellReuseIdentifier: "otherCell") 在代码中注册单元格?或者您是在 Storyboard Prototype 单元格中设置课程吗?

标签: ios swift xcode uitableview inheritance


【解决方案1】:

你还没有显示你的 borderView.addShadow(...) 函数在做什么,但这对我来说很好:

class ShadowCardTableViewCell: UITableViewCell {
    
    let borderView = UIView(frame: .zero)
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        changeToShadowedCard()
        // this line shouldn't be needed
        //self.contentView.layoutIfNeeded()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        // this line needed if setting the cell class as a Prototype in Storyboard
        changeToShadowedCard()
    }
    
    func changeToShadowedCard() {
        backgroundColor = .clear
        contentView.addSubview(borderView)
        borderView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            borderView.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: 10),
            borderView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: -10),
            borderView.rightAnchor.constraint(equalTo: self.contentView.rightAnchor, constant: -10),
            borderView.leftAnchor.constraint(equalTo: self.contentView.leftAnchor, constant: 10),
        ])
        borderView.backgroundColor = .secondarySystemBackground
        borderView.layer.cornerRadius = 7.5
        
        // not sure what you are doing here
        //borderView.addShadow(shadowColor: UIColor.label.cgColor, shadowOffset: CGSize(width: 0, height: 0), shadowOpacity: 0.3, shadowRadius: 4)

        // but this seems to work fine
        borderView.layer.shadowColor = UIColor.label.cgColor
        borderView.layer.shadowOffset = CGSize(width: 0, height: 0)
        borderView.layer.shadowOpacity = 0.3
        borderView.layer.shadowRadius = 4
    }
}

class OtherTableViewCell: ShadowCardTableViewCell {
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}

class SubCellVC: UITableViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // if you have not set your cell as a Storyboard Prototype
        //  register it here
        tableView.register(OtherTableViewCell.self, forCellReuseIdentifier: "otherCell")
        
        tableView.rowHeight = 60
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 20
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "otherCell", for: indexPath)
        return cell
    }

}

结果:

【讨论】:

    【解决方案2】:

    首先,您不能将 UITableViewCell 设为 UiViewController 的基类。 您可以做的是为您提供一个 UITableViewCell 扩展,类似于以下内容:

    extension UITableViewCell {
        func setShadow() {
            layer.shadowColor = UIColor.lightGray.cgColor
            layer.shadowOpacity = 0.25
            layer.borderColor = UIColor.separator.cgColor
            layer.borderWidth = 1.0
            layer.cornerRadius = 10
            layer.shadowRadius = 1.0
            layer.shadowOffset = CGSize(width: 1.0, height: 1.0)
        }
    }
    

    在注册一个单元并初始化它之后,调用它:

    cell.setShadow()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多