Skip to main content
 首页 » 操作系统

IOS Swift UITableView之无法解释的边距之如何摆脱

2024年01月01日4298°冷暖

所以我有一个带有自定义单元格的 UITableView...

这是单元格:

这是带有 UITableView 的 xib:

我无法解释蓝色单元格两侧的白色边缘???

我试过:

override func viewDidLoad() { 
    super.viewDidLoad() 
 
    ... 
 
    table.layoutMargins = UIEdgeInsetsZero 
    table.separatorInset = UIEdgeInsetsZero 
} 

我什至尝试了 iOS 8 UITableView separator inset 0 not working 的建议:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
 
    var cell:Cell_Jobs = self.table.dequeueReusableCellWithIdentifier("Cell_Jobs") as Cell_Jobs 
 
    cell.area.text = "Hello" 
 
 
    // kill insets for iOS 8 
    let nf = NSNumberFormatter() 
    let version = nf.numberFromString(UIDevice.currentDevice().systemVersion) 
 
    if(version?.floatValue >= 8){ 
        cell.preservesSuperviewLayoutMargins = false 
        cell.layoutMargins = UIEdgeInsetsZero 
    } 
    // iOS 7 and later 
    if(cell.respondsToSelector("setSeparatorInset:") ) { 
        cell.separatorInset = UIEdgeInsetsZero 
    } 
 
    return cell 
} 

但我仍然无法让左右边距消失......

我做错了什么?

XCode6.1.1。为 iOS 8 构建。

这是带有不需要的边距的麻烦输出:


@Teemu Kurppa 更新:

在 viewDidLoad() 中,我做了:

self.view.backgroundColor = UIColor.yellowColor() 
self.table.backgroundColor = UIColor.cyanColor() 

在 tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) 中,我做了:

cell.contentView.backgroundColor = UIColor.redColor() 

更新 2:

所以我成功了

  1. 我删除了父 UIView
  2. 我创建了一个新的 UIView
  3. 我重新添加了 UITableView 和 UIActivityIndi​​catorView

我从一个 Objective-C xib(一年多前写的)复制并粘贴 (Apple+C) 到一个新的 Swift xib - 我怀疑有一些有趣的设置挥之不去导致这些无法解释的边距。

谢谢大家的帮助,

请您参考如下方法:

对于单元格,试试这个:

override func tableView(tableView: UITableView,  
             willDisplayCell cell: UITableViewCell, 
      forRowAtIndexPath indexPath: NSIndexPath) 
{ 
    cell.separatorInset = UIEdgeInsetsZero 
    cell.preservesSuperviewLayoutMargins = false 
    cell.layoutMargins = UIEdgeInsetsZero 
} 

但根据编辑的信息,你似乎在 TableView 的容器 View 中有边距。这要么来自您在 TableView 和 View 之间的约束(一一检查),要么您在 View 中有布局边距,在这种情况下尝试;

 override func viewDidLoad() { 
     // ...  
     self.view.layoutMargins = UIEdgeInsetsZero 
 }