2016-07-23 58 views
0

工作,我想進行自定義的角落圓角背景爲我定製的細胞,並將其與下面的代碼的工作,但第一行是不會受到影響,我應該把它的滾動起來,使其有一個背景willDisplayCell第一行不斯威夫特

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 
     if indexPath.row != 0 { 
      cell.contentView.backgroundColor = UIColor.clearColor() 
      var whiteRoundedCornerView: UIView = UIView(frame: CGRectMake(10, 10, 365, 250)) 
      whiteRoundedCornerView.backgroundColor = UIColor.lightGrayColor() 
      whiteRoundedCornerView.layer.masksToBounds = false 
      whiteRoundedCornerView.layer.cornerRadius = 20 
      whiteRoundedCornerView.layer.shadowOffset = CGSizeMake(-0, -1) 
      whiteRoundedCornerView.layer.shadowOpacity = 0.1 

      cell.contentView.addSubview(whiteRoundedCornerView) 
      cell.contentView.sendSubviewToBack(whiteRoundedCornerView) 
     } 


    } 
+2

爲什麼你有'if indexPath.row!= 0'? – kabiroberai

+0

在這種情況下,我會將其作爲答案發布,以便其他人可以看到問題已解決 – kabiroberai

回答

0

代碼中的問題是if語句。當indexPath.row爲0(這是第一行)時,您的配置代碼不會執行。要解決這個問題,只需從代碼中刪除if聲明。它應該看起來像這樣:

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 
    cell.contentView.backgroundColor = UIColor.clearColor() 
    var whiteRoundedCornerView: UIView = UIView(frame: CGRectMake(10, 10, 365, 250)) 
    whiteRoundedCornerView.backgroundColor = UIColor.lightGrayColor() 
    whiteRoundedCornerView.layer.masksToBounds = false 
    whiteRoundedCornerView.layer.cornerRadius = 20 
    whiteRoundedCornerView.layer.shadowOffset = CGSizeMake(-0, -1) 
    whiteRoundedCornerView.layer.shadowOpacity = 0.1 

    cell.contentView.addSubview(whiteRoundedCornerView) 
    cell.contentView.sendSubviewToBack(whiteRoundedCornerView) 
}