【问题标题】:Table View Cell Background view keep getting brighter and brighter表格视图单元格背景视图越来越亮
【发布时间】: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 次

标签: iphone xcode swift2


【解决方案1】:

您的问题在于 WillDisplaycell 视图中的这一行

cell.contentView.addSubview(whiteRoundedView)

此行在每次显示单元格时添加 alpha 0.3 的白色视图,在前一个视图之上的多个视图导致更多的白色视图外观。

请在创建单元格时添加您的白色视图,即在

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)

let whiteRoundedView : UIView = cell.viewWithTag(1001/*Your tag*/)
red = CGColorCreate(CGColorSpaceCreateDeviceRGB(), [1.0, 1.0, 1.0, 0.3])
whiteRoundedView.layer.backgroundColor = red
whiteRoundedView.layer.masksToBounds = false
whiteRoundedView.layer.cornerRadius = 5.0

return cell
}

【讨论】:

  • 我不知道是什么问题,从昨天开始一直让我发疯
  • 你在使用原型单元吗?
  • 添加你想要的视图,给视图一个标签并在 cellForRowAtIndexPath 中使用,这样你就不需要将视图添加为子视图。这肯定会解决您的问题。
  • 您能否详细说明我如何使用代码来做到这一点.. 我的意思是添加我做了 view.tag = indexpath.row .. 在此之后要做什么
  • 你可以像 UIView *whiteVew = [cell viewWithTag:1000]; 一样访问你的视图休息 您可以根据需要使用此视图。无需再次将此视图添加为子视图
【解决方案2】:

请将该视图分配到表格视图方法之外。 您可以在视图中添加此加载。 然后在单元格的 contentView 中添加这个 View。

它可能会起作用..

【讨论】:

  • 你能给我看代码吗...因为你的建议不起作用
  • 我使用客观 c.
  • 这也无济于事。无论您在哪里初始化,如果您一次又一次地将其添加为子视图,都会导致相同的结果。
  • 请在开始时在 cellForRowAtIndexPath 中使用它...... for (UIView *vw in cell.contentView.subviews) { [vw removeFromSuperview]; }
【解决方案3】:

问题是由于出队问题。每当您在 cellForRowAtIndexPath 方法中添加子视图时,您必须检查该视图是否已添加为子视图的条件,如果已添加,则必须删除子视图并再次添加。

【讨论】:

    猜你喜欢
    • 2020-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多