willDisplayCell第一行在Swift中不起作用

时间:2016-07-23 12:22:19

标签: swift uitableview

我想为我的自定义单元格制作自定义角圆角背景,并且它使用下面的代码,但第一行不受影响我应该向上滚动以使其具有背景

 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)
        }


    }

1 个答案:

答案 0 :(得分:0)

代码中的问题是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)
}