swift - willDisplayCell 第一行在 Swift 中不起作用

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


    }

最佳答案

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

相关文章:

swift - == 和 === 的区别

ios - 添加 viewController 作为 subview Swift 不显示任何组件

ios - UITableViewCell 删除按钮不消失

iphone - 这个 UI 元素是什么

swift - 在 Swift 3 中很好地显示 JSON 数据

ios - Controller 在应用程序访问之前就已初始化

swift - 为什么我不能在 Swift 中将可选数组转换为父类(super class)?

ios - 如何将 UITableView 或 UITableViewController 集成到 UIPageViewController 中?

iphone - 我有一个 iPad 应用程序,由于未捕获的异常 'NSInternalInconsistencyException',不断出现终止应用程序的情况

objective-c - 如何从表格 View Controller 中删除分隔符