2017-07-31 100 views
0

我在我的tableview單元格周圍添加了空白區域,每當我滾動時滾動此陰影越來越大,並且在滾動第二和第三個時出現滯後時間與更大的影子向tableview單元格添加陰影造成滯後滾動和複製陰影

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomCell 
    cell.backgroundColor = UIColor.clear 
    cell.contentView.backgroundColor = UIColor.clear 

    let whiteRoundedView : UIView = UIView(frame: CGRect(x:10,y: 5,width: self.view.frame.size.width - 20,height: 117)) 

    whiteRoundedView.layer.backgroundColor = CGColor(colorSpace: CGColorSpaceCreateDeviceRGB(), components: [1.0, 1.0, 1.0, 0.9]) 
    //whiteRoundedView.layer.masksToBounds = true 
    whiteRoundedView.layer.cornerRadius = 5.0 
    whiteRoundedView.layer.shadowOffset = CGSize(width: -1,height: 1) 
    whiteRoundedView.layer.shadowOpacity = 0.2 

    let shadowPath = UIBezierPath(rect: whiteRoundedView.layer.bounds) 
    whiteRoundedView.layer.shouldRasterize = true 
    whiteRoundedView.layer.shadowPath = shadowPath.cgPath 
    cell.contentView.addSubview(whiteRoundedView) 
    cell.contentView.sendSubview(toBack: whiteRoundedView) 

    return cell 
} 
+0

從nib方法在自定義單元類awak上放置陰影代碼 –

+0

您應該在自定義單元類中添加陰影。這會在每次滾動時增加陰影 – Phyber

+0

我想這樣做,但我怎麼能這樣做? – UncleJunior

回答

3

只要把代碼中的awakefrom筆尖

class CustomCell: UITableViewCell { 



     override func awakeFromNib() { 
      super.awakeFromNib() 
      // Initialization code 
    self.backgroundColor = UIColor.clear 
     self.contentView.backgroundColor = UIColor.clear 

     let whiteRoundedView : UIView = UIView(frame: CGRect(x:10,y: 5,width: self.contentView.frame.size.width - 20,height: 117)) 

     whiteRoundedView.layer.backgroundColor = CGColor(colorSpace: CGColorSpaceCreateDeviceRGB(), components: [1.0, 1.0, 1.0, 0.9]) 
     //whiteRoundedView.layer.masksToBounds = true 
     whiteRoundedView.layer.cornerRadius = 5.0 
     whiteRoundedView.layer.shadowOffset = CGSize(width: -1,height: 1) 
     whiteRoundedView.layer.shadowOpacity = 0.2 

     let shadowPath = UIBezierPath(rect: whiteRoundedView.layer.bounds) 
     whiteRoundedView.layer.shouldRasterize = true 
     whiteRoundedView.layer.shadowPath = shadowPath.cgPath 
     self.contentView.addSubview(whiteRoundedView) 
     self.contentView.sendSubview(toBack: whiteRoundedView) 

     } 


    } 
1

您不斷添加陰影視圖在彼此的頂部,而不必刪除它們。如果你所有的細胞都需要的陰影,你可以只添加一個標籤,並檢查是否與標籤視圖已經存在,像這樣:

whiteRoundedView.tag = 12345 

if cell.contentView.viewWithTag(12345) == nil { 
    cell.contentView.addSubview(whiteRoundedView) 
} 

如果某些細胞有陰影,但有些細胞不,你能做到這一點像這樣:

if let shadowView = cell.contentView.viewWithTag(12345) && noShadow { 
    shadowView.removeFromSuperview 
} else if !noShadow { 
    cell.contentView.addSubview(whiteRoundedView) 
} 

或者像你將它添加到您的自定義單元格類問題的意見中提到:

override func awakeFromNib() { 
    super.awakeFromNib() 

    let whiteRoundedView : UIView = UIView(frame: CGRect(x:10,y: 5,width: self.contentView.frame.size.width - 20,height: 117)) 

    whiteRoundedView.layer.backgroundColor = CGColor(colorSpace: CGColorSpaceCreateDeviceRGB(), components: [1.0, 1.0, 1.0, 0.9]) 
    whiteRoundedView.layer.cornerRadius = 5.0 
    whiteRoundedView.layer.shadowOffset = CGSize(width: -1,height: 1) 
    whiteRoundedView.layer.shadowOpacity = 0.2 

    let shadowPath = UIBezierPath(rect: whiteRoundedView.layer.bounds) 
    whiteRoundedView.layer.shouldRasterize = true 
    whiteRoundedView.layer.shadowPath = shadowPath.cgPath 
    self.contentView.addSubview(whiteRoundedView) 
} 
+0

喚醒時,謝謝第二個解決方案似乎工作完美無缺 – UncleJunior

0

裏面卻whiteRoundedView每次增加。如果您正在使用故事板或筆尖來設計Cell UI,那麼您可以在其中添加whiteRoundedView。或者,您可以在whiteRoundedView中添加標籤,並在將whiteRoundedView作爲子視圖添加之前,檢查是否已經添加了該標籤的任何視圖。