2017-02-07 6 views
-1

私はこの状況に対処しています。再利用可能なセルを持つtableViewを作成しました。そのセルをシャドーで設定しましたが、すべてがOKですが、セルを2回タップするとセルが再びペイントされます。私が望むことをしたくないのは、ビューの最初に表示されているセルが最初のビューであり、2番目のビューがセルをタップしたときのビューです。セルをタップしても表示されます。私が欲しいのは、最初のように細胞がそのままであるということです。セルを設定する方法

ここに私のコードと私の意見があります。ありがとう。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell! 
    cell.textLabel?.text = self.sodas[indexPath.row] 


    cell.layer.borderColor = UIColor.lightGray.cgColor 
    cell.layer.cornerRadius = 8 
    cell.layer.shadowOffset = CGSize(width: 5, height: 20) 
    cell.layer.shadowColor = UIColor.black.cgColor 
    cell.layer.shadowRadius = 1 
    cell.layer.shadowOpacity = 0.6 

    cell.clipsToBounds = false 

    let shadowFrame: CGRect = (cell.layer.bounds) 
    let shadowPath: CGPath = UIBezierPath(rect: shadowFrame).cgPath 
    cell.layer.shadowPath = shadowPath 

    return cell 
} 

first view

second view

答えて

3

セルの枠がcellForRowAtに設定されていません。それは早すぎる。セルのシャドウフレームを設定するには、tableView(_:willDisplay:forRowAt:)デリゲートメソッドを使用する必要があります。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell! 
    cell.textLabel?.text = self.sodas[indexPath.row] 

    cell.layer.borderColor = UIColor.lightGray.cgColor 
    cell.layer.cornerRadius = 8 
    cell.layer.shadowOffset = CGSize(width: 5, height: 20) 
    cell.layer.shadowColor = UIColor.black.cgColor 
    cell.layer.shadowRadius = 1 
    cell.layer.shadowOpacity = 0.6 

    cell.clipsToBounds = false 

    return cell 
} 

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 
    let shadowFrame: CGRect = (cell.layer.bounds) 
    let shadowPath: CGPath = UIBezierPath(rect: shadowFrame).cgPath 
    cell.layer.shadowPath = shadowPath 
} 
関連する問題