【发布时间】:2016-10-20 15:22:32
【问题描述】:
嘿,我遇到了这个奇怪的问题,我在表格视图上添加了一个子视图并给它一个背景颜色。当应用程序启动时,颜色就像在图像中一样好
但是当我向下滚动时,边缘下方的单元格会比当前单元格亮一点 像这样
向后滚动后,第一个单元格变得更亮,多次滚动后变为完全白色。
这是表格视图单元格的代码
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("newsCell", forIndexPath: indexPath) as! EventsViewCell
let dict = componentsLoaded[indexPath.row] as! NSDictionary
cell.eventTitle!.text = (dict["news_headline"] as! String)
cell.backgroundColor = UIColor.clearColor()
let date_post = dict["news_date"] as! String
cell.dateLabel!.text = ConvertDate().convertYourDate(date_post)
return cell
}
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
let height = cell.frame.height
cell.contentView.backgroundColor = UIColor.clearColor()
let whiteRoundedView : UIView = UIView(frame: CGRectMake(10, 10, self.view.frame.size.width - 20, height ))
red = CGColorCreate(CGColorSpaceCreateDeviceRGB(), [1.0, 1.0, 1.0, 0.3])
whiteRoundedView.layer.backgroundColor = red
whiteRoundedView.layer.masksToBounds = false
whiteRoundedView.layer.cornerRadius = 5.0
cell.contentView.addSubview(whiteRoundedView)
cell.contentView.sendSubviewToBack(whiteRoundedView)
}
【问题讨论】:
-
用 3dview 调试你的布局,我猜你的白色视图被添加了多次导致这个问题。
-
是的,你是对的..如何避免这种情况?我的视图在单个单元格上添加了 4 次