Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在我的自定义表格视图单元格中添加CAGradientLayer的正确位置是什么?_Ios_Swift_Uitableview - Fatal编程技术网

Ios 在我的自定义表格视图单元格中添加CAGradientLayer的正确位置是什么?

Ios 在我的自定义表格视图单元格中添加CAGradientLayer的正确位置是什么?,ios,swift,uitableview,Ios,Swift,Uitableview,我有一个带有自定义表格视图单元格的表格。每个表视图单元格都有一个背景图像,并在图像顶部加上一些文本。我想在图像视图上方添加一个渐变,以便图像上方的文本更易于阅读 添加此渐变的正确位置在哪里 我目前正在CellForRowatineXpath中添加此代码,我正在粘贴下面的代码。。数组CellShowed跟踪是否显示此单元格 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&g

我有一个带有自定义表格视图单元格的表格。每个表视图单元格都有一个背景图像,并在图像顶部加上一些文本。我想在图像视图上方添加一个渐变,以便图像上方的文本更易于阅读

添加此渐变的正确位置在哪里

我目前正在CellForRowatineXpath中添加此代码,我正在粘贴下面的代码。。数组CellShowed跟踪是否显示此单元格

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("AirportPlaceListTableViewCell") as! AirportPlaceListTableViewCell
    cell.titleLabel.text = tableData[indexPath.row] as? String
    let imageName = tableImageData[indexPath.row] as? String
    let image = UIImage(named: imageName!)
    cell.placeImageView.image = TripnaryHelper.imageWithImage(image, scaledToSize: CGSizeMake(320, 320))

    if cellShown[indexPath.row] == false
    {
        let gradient = CAGradientLayer.init()
        gradient.frame = cell.bounds
        let bottomColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5)
        gradient.colors = [UIColor.clearColor().CGColor, bottomColor.CGColor]
        gradient.startPoint = CGPoint(x: 0, y: 0.66)
        gradient.endPoint = CGPoint(x: 0, y: 1.0)
        cell.placeImageView.layer .addSublayer(gradient)
        cellShown[indexPath.row] = true
    }

    return cell
}
这里发生的事情是,当细胞被重用时,梯度又被添加了。所以我的问题是 -我该怎么查呢?
-或者,还有其他地方可以为我的自定义表格视图单元格类添加此渐变吗?

您应该在自定义单元格(
AirportPlaceListTableViewCell
)覆盖的初始化方法中添加
CAGradientLayer

init(style style: UITableViewCellStyle, reuseIdentifier reuseIdentifier: String?)
但是,如果您已在
xib
中构建了自定义单元格,则需要在加载
xib
后添加
cagradintlayer
。在这种情况下,您可以在
AirportPlaceListTableViewCell
中执行此操作:

func awakeFromNib()

谢谢@simeon,这很有魅力。我将其添加到
func awakeFromNib()