我有一个应用程序,其中UITableView的分隔符插入设置为自定义值-右0,左0。这在iOS 7中非常有效。x,但是在iOS 8.0中,我看到右边的分隔符被设置为默认值15。即使在xib文件中它设置为0,它仍然不正确地显示。
如何删除UITableViewCell分隔符边距?
我有一个应用程序,其中UITableView的分隔符插入设置为自定义值-右0,左0。这在iOS 7中非常有效。x,但是在iOS 8.0中,我看到右边的分隔符被设置为默认值15。即使在xib文件中它设置为0,它仍然不正确地显示。
如何删除UITableViewCell分隔符边距?
当前回答
使用下面的代码片段避免不必要的填充问题UITableView在IOS 8和7。
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if ([tableView respondsToSelector:@selector(setSeparatorInset:)])
{
[tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([tableView respondsToSelector:@selector(setLayoutMargins:)])
{
[tableView setLayoutMargins:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)])
{
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
其他回答
你可以使用一次UIAppearance,在你的应用程序启动时(在UI被加载之前),把它设置为默认的全局设置:
// iOS 7:
[[UITableView appearance] setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
[[UITableView appearance] setSeparatorInset:UIEdgeInsetsZero];
[[UITableViewCell appearance] setSeparatorInset:UIEdgeInsetsZero];
// iOS 8:
if ([UITableView instancesRespondToSelector:@selector(setLayoutMargins:)]) {
[[UITableView appearance] setLayoutMargins:UIEdgeInsetsZero];
[[UITableViewCell appearance] setLayoutMargins:UIEdgeInsetsZero];
[[UITableViewCell appearance] setPreservesSuperviewLayoutMargins:NO];
}
这样,你可以保持你的UIViewController的代码干净,如果你想重写它。
Arg ! !在你的Cell子类中做这些事情后:
- (UIEdgeInsets)layoutMargins
{
return UIEdgeInsetsZero;
}
或者设置单元格。layoutmargin = UIEdgeInsetsZero;帮我修好了。
大多数答案是显示分隔符插入和布局边距设置在各种方法(即viewDidLayoutSubviews, willDisplayCell等)的单元格和表视图,但我发现,只是把这些cellForRowAtIndexPath工作得很好。似乎是最干净的方法。
// kill insets for iOS 8
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8) {
cell.preservesSuperviewLayoutMargins = NO;
[cell setLayoutMargins:UIEdgeInsetsZero];
}
// iOS 7 and later
if ([cell respondsToSelector:@selector(setSeparatorInset:)])
[cell setSeparatorInset:UIEdgeInsetsZero];
这是在Swift中为我工作的代码:
override func viewDidLoad()
{
super.viewDidLoad()
...
if tableView.respondsToSelector("setSeparatorInset:") {
tableView.separatorInset = UIEdgeInsetsZero
}
}
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,forRowAtIndexPath indexPath: NSIndexPath)
{
if cell.respondsToSelector("setSeparatorInset:") {
cell.separatorInset.left = CGFloat(0.0)
}
if tableView.respondsToSelector("setLayoutMargins:") {
tableView.layoutMargins = UIEdgeInsetsZero
}
if cell.respondsToSelector("setLayoutMargins:") {
cell.layoutMargins.left = CGFloat(0.0)
}
}
这对我来说似乎是最干净的(目前),因为所有的单元格/tableView边缘/边缘调整都是在tableView:willDisplayCell:forRowAtIndexPath:方法中完成的,而不是在tableView:cellForRowAtIndexPath:中填充不必要的代码。
顺便说一句,我只是设置单元格的左separatorInset/ layoutmargin,因为在这种情况下,我不想搞砸我在单元格中设置的约束。
代码更新到Swift 2.2:
override func viewDidLoad() {
super.viewDidLoad()
if tableView.respondsToSelector(Selector("setSeparatorInset:")) {
tableView.separatorInset = UIEdgeInsetsZero
}
}
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,forRowAtIndexPath indexPath: NSIndexPath) {
if cell.respondsToSelector(Selector("setSeparatorInset:")) {
cell.separatorInset.left = CGFloat(0.0)
}
if tableView.respondsToSelector(Selector("setLayoutMargins:")) {
tableView.layoutMargins = UIEdgeInsetsZero
}
if cell.respondsToSelector(Selector("setLayoutMargins:")) {
cell.layoutMargins.left = CGFloat(0.0)
}
}
Swift 2.0扩展
我只是想分享一个扩展,我做了删除边缘从tableview单元格分隔符。
extension UITableViewCell {
func removeMargins() {
if self.respondsToSelector("setSeparatorInset:") {
self.separatorInset = UIEdgeInsetsZero
}
if self.respondsToSelector("setPreservesSuperviewLayoutMargins:") {
self.preservesSuperviewLayoutMargins = false
}
if self.respondsToSelector("setLayoutMargins:") {
self.layoutMargins = UIEdgeInsetsZero
}
}
}
用于上下文中:
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCell
cell.removeMargins()
return cell