我正在使用表视图控制器中的自定义表视图单元在iOS6中制作应用。该单元是使用情节提要中的原型单元设计的。
在表视图控制器中,我正在做三件事:
问题是,加载表格视图时,最初显示的3个单元格会正确显示动画和圆角,但没有阴影效果。但是,当我向下滚动时,出现的新单元格也具有动画+圆角+阴影。
而且,当我向上滚动时,最初的3个单元格也具有阴影效果。
调试了几个小时,这让我更加无能为力。有什么建议吗?
我解决了问题。 [单元格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()
}
然后在视图控制器内部实现willDisplayCell
委托方法,将单元格强制转换为您的单元格子类,并在主线程上调用cell.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()
}
}