UITableView
仅在 iOS 7 中存在一些奇怪的问题。
UITableViewCellSeparator
在第一行上方和最后一行下方消失。有时在选择行或一些滚动操作后会出现。
在我的例子中,tableView
是从 Storyboard
加载的,带有 UITableViewStylePlain
样式。问题肯定不在 UITableViewCellSeparatorStyle
中,它与默认的 UITableViewCellSeparatorStyleSingleLine
没有改变。
正如我在 Apple Dev Forums( here 和 here )上看到的,其他人也有这样的问题,并找到了一些解决方法,例如:
Workaround: disable the default selection and recreate the behaviour in a method
trigged by a tapGestureRecognizer.
但我仍在寻找这种分隔符奇怪行为的原因。
有什么想法吗?
更新:正如我在 XCode 5.1 DP 和 iOS 7.1 beta 中看到的,Apple 试图解决这个问题。现在分隔符有时会根据需要显示在最后一行下方,经过一些刷新,但不是在创建 tableview 之后。
最佳答案
我转储了受影响单元格的 subview 层次结构,发现 _UITableViewCellSeparatorView
被设置为隐藏。难怪不显示!
我在 UITableViewCell
子类中覆盖了 layoutSubviews
,现在分隔符可以可靠地显示:
Objective-C:
- (void)layoutSubviews {
[super layoutSubviews];
for (UIView *subview in self.contentView.superview.subviews) {
if ([NSStringFromClass(subview.class) hasSuffix:@"SeparatorView"]) {
subview.hidden = NO;
}
}
}
swift :
override func layoutSubviews() {
super.layoutSubviews()
guard let superview = contentView.superview else {
return
}
for subview in superview.subviews {
if String(subview.dynamicType).hasSuffix("SeparatorView") {
subview.hidden = false
}
}
}
这里提出的其他解决方案对我来说并不能始终如一地工作或看起来很笨重(添加自定义 1 px 页脚 View )。
https://stackoverflow.com/questions/18924589/