Ios 滚动后将显示自定义UITableViewCell阴影
我正在iOS6中使用表视图控制器中的自定义表视图单元格制作一个应用程序。单元是使用故事板中的原型单元设计的 在表视图控制器中,我要做三件事:Ios 滚动后将显示自定义UITableViewCell阴影,ios,uitableview,quartz-core,Ios,Uitableview,Quartz Core,我正在iOS6中使用表视图控制器中的自定义表视图单元格制作一个应用程序。单元是使用故事板中的原型单元设计的 在表视图控制器中,我要做三件事: 将简单自定义动画添加到tableView:willDisplayCell:forRowAtIndexPath: 将圆角效果添加到tableView中的单元格:cellForRowAtIndexPath: 在tableView:cellForRowAtIndexPath中向单元格添加阴影效果: 问题是,加载表格视图时,最初出现的3个单元格将正确显示动画和圆角
几个小时的调试让我更加不知所措。有什么建议吗?我解决了这个问题。[cell layoutSubviews]完成我需要的所有操作:
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.imageView.layer.masksToBounds = NO;
cell.imageView.layer.cornerRadius = 5.0f;
[cell layoutSubviews];
}
cell.imageView.layer.shadowOpacity = 0.5f;
cell.imageView.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:cell.imageView.bounds cornerRadius:5.0f].CGPath;
cell.imageView.layer.shadowOffset = CGSizeMake(2.5f, 2.5f);
cell.imageView.layer.shadowRadius = 2.5f;
如果其他人有此问题: 在
UITableViewCell
子类中,重写layoutSubviews
,然后插入添加阴影的代码。最后,确保调用super.layoutSubviews()
作为最后一行代码
override func layoutSubviews()
configureShadow() // <-- insert shadow code here, or create a method
super.layoutSubviews()
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let _cell = cell as! CustomCell // Replace CustomCell with your cell subclass
DispatchQueue.main.async {
_cell.layoutSubviews()
}
}