我正在处理这种情况,我创建了一个带有可重复使用单元格的tableView,我设置了带阴影的单元格,一切正常,但是当我点击一个单元格两次单元格再次绘制单元格时,我不会#39 ; t想要这个我想要的是开头视图中显示的单元格,第一个屏幕是第一个视图,第二个是我点击一个单元格时的视图,我想,无论我是否点击一个单元格,我都不知道#39; t想要阴影增加,我想要的是那个单元格保持原样。
这是我的代码和观点。谢谢。
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
}
答案 0 :(得分: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
}