2015-11-04 140 views
0

我有一个具有阴影单元的UITableView。当我上下滚动时,单元格的阴影消失。正如我认为这是一个重复使用问题,我已经只用过这个单元格了。这里可能存在什么问题?UITableViewCell上的阴影在滚动时消失

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

     var cell = UITableViewCell() 

     switch indexPath.row { 
     case 3: cell = shadowBasicCellAtIndexPath(indexPath) 
     case 4: cell = contentCellAtIndexPath(indexPath) 
     case 5: cell = contentCellAtIndexPath(indexPath) 
     case 6: cell = contentCellAtIndexPath(indexPath) 
     case 11: cell = contentCellAtIndexPath(indexPath) 
     default: cell = basicCellAtIndexPath(indexPath) 
     } 

     return cell 
    } 



    func contentCellAtIndexPath(indexPath: NSIndexPath) -> ContentCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier(contentCellIdentifier) as! ContentCell 
     setTitleForCell(cell, indexPath: indexPath) 
     setContentForCell(cell, indexPath: indexPath) 
     return cell 
    } 

    func shadowBasicCellAtIndexPath(indexPath: NSIndexPath) -> ShadowBasicCell { 

     // let cell = tableView.dequeueReusableCellWithIdentifier(shadowBasicCellIdentifier) as! ShadowBasicCell 
     let cell = tableView.dequeueReusableCellWithIdentifier(shadowBasicCellIdentifier, forIndexPath: indexPath) as! ShadowBasicCell 

     // let cell = ShadowBasicCell(style: <#T##UITableViewCellStyle#>, reuseIdentifier: <#T##String?#>) 

     cell.icon.image = upperTableIcons[indexPath.row] 
     cell.textlabel.text = upperTableLabels[indexPath.row] 
     cell.textlabel.textColor = UIColor.dmvBody1() 
     cell.textlabel.font = UIFont.dmvBody1() 
     cell.valueLabel.font = UIFont.dmvBody1() 
     cell.selectionStyle = UITableViewCellSelectionStyle.None 


     cell.layer.shadowColor = UIColor.blackColor().CGColor 
     cell.layer.shadowOffset = CGSizeMake(5, 5); 
     cell.layer.shadowOpacity = 0.2; 
     cell.layer.shadowRadius = 3.0; 
     cell.clipsToBounds = false 

     let shadowFrame: CGRect = (cell.layer.bounds) 
     let shadowPath: CGPathRef = UIBezierPath(rect: shadowFrame).CGPath 
     cell.layer.shadowPath = shadowPath 
     cell.separatorInset = UIEdgeInsets.init(top: 0, left: cell.frame.width, bottom: 0, right: 0) 
     return cell 

    } 

    func basicCellAtIndexPath(indexPath: NSIndexPath) -> BasicCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier(basicCellIdentifier) as! BasicCell 
     cell.icon.image = upperTableIcons[indexPath.row] 
     cell.textlabel.text = upperTableLabels[indexPath.row] 
     cell.textlabel.textColor = UIColor.dmvBody1() 
     cell.valueLabel.text = upperTableValues[indexPath.row] 
     cell.valueLabel.textColor = UIColor.dmvBody1() 
     cell.textlabel.font = UIFont.dmvBody1() 
     cell.valueLabel.font = UIFont.dmvBody1() 
     cell.selectionStyle = UITableViewCellSelectionStyle.None 
     if indexPath.row > 6 {cell.backgroundColor = UIColor.dmvBeige30()} else { 
      cell.backgroundColor = UIColor.whiteColor() 
     } 
     return cell 
    } 
+0

你的代码乍看之下看起来很好(尽管你的'switch'语句具有相同的'case'重复是非常愚蠢的)。但是这里有一些建议:不要让它的图层蒙上阴影,关闭“clipsToBounds”。取而代之的是,以这样的方式去除细胞中的阴影,使其看起来像细胞正在投射阴影。你这样做的方式,滚动会结束,因为你正在使渲染系统工作。 – matt

+0

好的。我如何画一个阴影? –

回答

1

我解决了自己的问题:阴影并没有看到的原因,是因为它覆盖槽下,进行了重绘。我将下列单元格的背景颜色设置为clearColor(),现在一切正常。