在 tableView 的单元格上,我正在尝试使用以下 Swift 代码定义渐变:
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
var cell:UITableViewCell = self.tableView?.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
cell.backgroundColor=UIColor.clearColor()
let gradient : CAGradientLayer = CAGradientLayer()
var arrayColors:Array<AnyObject> = [UIColor.blackColor().CGColor, UIColor.whiteColor().CGColor]
gradient.colors=arrayColors
gradient.frame = cell.bounds
cell.layer.insertSublayer(gradient, atIndex: UInt32(indexPath.row))
return cell
}
行为非常混乱:第一次加载视图时,所有单元格都有白色背景。如果我上下滚动,一些内容的单元格消失(标题和副标题),渐变最终出现在这些单元格中(随机地,其他一些单元格保持白色背景)。是否与此演员表有关 -> UInt32(indexPath.row) ?