2015-11-04 141 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(),現在一切正常。