willDisplayCell第一行不斯威夫特
问题描述:
工作,我想进行自定义的角落圆角背景为我定制的细胞,并将其与下面的代码的工作,但第一行是不会受到影响,我应该把它的滚动起来,使其有一个背景willDisplayCell第一行不斯威夫特
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)
}
为什么你有'if indexPath.row!= 0'? – kabiroberai
在这种情况下,我会将其作为答案发布,以便其他人可以看到问题已解决 – kabiroberai