我想为我的自定义单元格制作自定义角圆角背景,它与下面的代码一起使用,但第一行不受影响,我应该向上滚动它以使其具有背景
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.row != 0 {
cell.contentView.backgroundColor = UIColor.clearColor()
var whiteRoundedCornerView: UIView = UIView(frame: CGRectMake(10, 10, 365, 250))
whiteRoundedCornerView.backgroundColor = UIColor.lightGrayColor()
whiteRoundedCornerView.layer.masksToBounds = false
whiteRoundedCornerView.layer.cornerRadius = 20
whiteRoundedCornerView.layer.shadowOffset = CGSizeMake(-0, -1)
whiteRoundedCornerView.layer.shadowOpacity = 0.1
cell.contentView.addSubview(whiteRoundedCornerView)
cell.contentView.sendSubviewToBack(whiteRoundedCornerView)
}
}
最佳答案
代码中的问题是 if
语句。当 indexPath.row
为 0(即第一行)时,您的配置代码不会执行。要解决此问题,只需从代码中删除 if
语句即可。它应该看起来像这样:
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.contentView.backgroundColor = UIColor.clearColor()
var whiteRoundedCornerView: UIView = UIView(frame: CGRectMake(10, 10, 365, 250))
whiteRoundedCornerView.backgroundColor = UIColor.lightGrayColor()
whiteRoundedCornerView.layer.masksToBounds = false
whiteRoundedCornerView.layer.cornerRadius = 20
whiteRoundedCornerView.layer.shadowOffset = CGSizeMake(-0, -1)
whiteRoundedCornerView.layer.shadowOpacity = 0.1
cell.contentView.addSubview(whiteRoundedCornerView)
cell.contentView.sendSubviewToBack(whiteRoundedCornerView)
}
关于swift - willDisplayCell 第一行在 Swift 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38541741/