【发布时间】:2014-10-22 18:38:03
【问题描述】:
在 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) ?
【问题讨论】:
-
尝试使用@IBOutlet var table 添加您的tableview:UITableView!并在你的 func tableView 添加 table.reloadData()
-
看看这个:github.com/rlease/GradientTableExample - 发现上次我正在寻找类似的东西。也许有帮助。
-
您可能忘记明确定义颜色的
locations。我知道这只是可选的,但为了安全起见,你知道;您也不需要向重复使用的单元格添加新的渐变层,该单元已经具有渐变层;并且该层应该插入到第 0 个索引中,我不明白您为什么尝试将其插入到rowth 索引处。那有什么意义呢?
标签: ios swift tableview gradient cagradientlayer