我决定使用xib声明一个自定义的UITableViewCell类,以便更容易控制我的单元格的背景,因为它需要根据表格中的单元格位置而有所不同。
这很好用,但当我尝试在故事板中为我的自定义单元格添加额外的视图时,它们不会显示,只显示原始的自定义单元格。
这是我的自定义单元格类:
class GlTableViewCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
let bgView: UIView = UIView()
bgView.backgroundColor = .clear
self.backgroundView = bgView
self.backgroundColor = .clear
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func isFirst() {
//Top
let topLeftCornerImageView = self.viewWithTag(101) as! UIImageView
let topLineImageView = self.viewWithTag(102) as! UIImageView
let topRightCornerImageView = self.viewWithTag(103) as! UIImageView
topLeftCornerImageView.image = UIImage(named: "cellTopLeftCorner")
topLineImageView.image = UIImage(named: "cellTopLine")
topRightCornerImageView.image = UIImage(named: "cellTopRightCorner")
//Bottom
let bottomLeftCornerImageView = self.viewWithTag(201) as! UIImageView
let bottomLineImageView = self.viewWithTag(202) as! UIImageView
let bottomRoghtCornerImageView = self.viewWithTag(203) as! UIImageView
bottomLeftCornerImageView.image = UIImage(named: "cellLeftLine")
bottomLineImageView.image = UIImage(named: "cellBackgroundFill")
bottomRoghtCornerImageView.image = UIImage(named: "cellRightLine")
}
func isLast() {
//Top
let topLeftCornerImageView = self.viewWithTag(101) as! UIImageView
let topLineImageView = self.viewWithTag(102) as! UIImageView
let topRightCornerImageView = self.viewWithTag(103) as! UIImageView
topLeftCornerImageView.image = UIImage(named: "cellLeftLine")
topLineImageView.image = UIImage(named: "cellBackgroundFill")
topRightCornerImageView.image = UIImage(named: "cellRightLine")
//Bottom
let bottomLeftCornerImageView = self.viewWithTag(201) as! UIImageView
let bottomLineImageView = self.viewWithTag(202) as! UIImageView
let bottomRoghtCornerImageView = self.viewWithTag(203) as! UIImageView
bottomLeftCornerImageView.image = UIImage(named: "cellBottomLeftCorner")
bottomLineImageView.image = UIImage(named: "cellBottomLine")
bottomRoghtCornerImageView.image = UIImage(named: "cellBottomRightCorner")
}
func isSingle() {
//Top
let topLeftCornerImageView = self.viewWithTag(101) as! UIImageView
let topLineImageView = self.viewWithTag(102) as! UIImageView
let topRightCornerImageView = self.viewWithTag(103) as! UIImageView
topLeftCornerImageView.image = UIImage(named: "cellTopLeftCorner")
topLineImageView.image = UIImage(named: "cellTopLine")
topRightCornerImageView.image = UIImage(named: "cellTopRightCorner")
//Bottom
let bottomLeftCornerImageView = self.viewWithTag(201) as! UIImageView
let bottomLineImageView = self.viewWithTag(202) as! UIImageView
let bottomRoghtCornerImageView = self.viewWithTag(203) as! UIImageView
bottomLeftCornerImageView.image = UIImage(named: "cellBottomLeftCorner")
bottomLineImageView.image = UIImage(named: "cellBottomLine")
bottomRoghtCornerImageView.image = UIImage(named: "cellBottomRightCorner")
}
func isMiddle() {
let topLeftCornerImageView = self.viewWithTag(101) as! UIImageView
let topLineImageView = self.viewWithTag(102) as! UIImageView
let topRoghtCornerImageView = self.viewWithTag(103) as! UIImageView
let bottomLeftCornerImageView = self.viewWithTag(201) as! UIImageView
let bottomLineImageView = self.viewWithTag(202) as! UIImageView
let bottomRightCornerImageView = self.viewWithTag(203) as! UIImageView
topLeftCornerImageView.image = UIImage(named: "cellLeftLine")
topLineImageView.image = UIImage(named: "cellBackgroundFill")
topRoghtCornerImageView.image = UIImage(named: "cellRightLine")
bottomLeftCornerImageView.image = UIImage(named: "cellLeftLine")
bottomLineImageView.image = UIImage(named: "cellBackgroundFill")
bottomRightCornerImageView.image = UIImage(named: "cellRightLine")
}
}
这是我的表vc:
override func viewDidLoad() {
super.viewDidLoad()
updateLabels()
tableView.register(UINib(nibName: "GlTableViewCell", bundle: nil), forCellReuseIdentifier: "ProductCell")
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ProductCell", for: indexPath) as! GlTableViewCell
cell.isSingle()
return cell
..
我做错了什么?