2017-02-07 136 views
-1

我正在处理这种情况,我创建了一个带有可重用单元格的tableView,我用阴影设置了该单元格,一切正常,但是当我点击一个单元格两次时单元格被再次绘制,我不希望这是我想要的是开始时视图中显示的单元格,第一个屏幕是第一个视图,第二个是我点击一个单元格时的视图,我想,不管是否点击一个单元格,我不想要那个阴影增加,我想要的是细胞保持原样。如何设置单元格

这是我的代码和我的意见。谢谢。

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 
} 
相关问题