我有一个应用程序,其中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分隔符边距?
当前回答
迅速:
override func viewDidLoad() {
super.viewDidLoad()
if self.tableView.respondsToSelector("setSeparatorInset:") {
self.tableView.separatorInset = UIEdgeInsetsZero
}
if self.tableView.respondsToSelector("setLayoutMargins:") {
self.tableView.layoutMargins = UIEdgeInsetsZero
}
self.tableView.layoutIfNeeded() // <--- this do the magic
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
...
if cell.respondsToSelector("setSeparatorInset:") {
cell.separatorInset = UIEdgeInsetsZero
}
if cell.respondsToSelector("setLayoutMargins:") {
cell.layoutMargins = UIEdgeInsetsZero
}
return cell
}
其他回答
在Swift中,这有点烦人,因为layoutmargin是一个属性,所以你必须重写getter和setter。
override var layoutMargins: UIEdgeInsets {
get { return UIEdgeInsetsZero }
set(newVal) {}
}
这将有效地使layoutmargin为只读,这在我的情况下是好的。
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
简单地把这两行放在cellForRowAtIndexPath方法中
如果你想要所有的分隔线都从0开始 (细胞setSeparatorInset: UIEdgeInsetsZero); (细胞setLayoutMargins: UIEdgeInsetsZero);
如果你想特定的分隔线是从0开始假设这是最后一行是从0开始
if (indexPath.row == array.count-1)
{
[cell setSeparatorInset:UIEdgeInsetsZero];
[cell setLayoutMargins:UIEdgeInsetsZero];
}
else
tblView.separatorInset=UIEdgeInsetsMake(0, 10, 0, 0);
这就是我的解。这适用于自定义单元格子类,只需将它们都添加到子类。
- (UIEdgeInset)layoutMargins { 返回 UIEdgeInsetsMake(0, 10, 0, 10); }
2.
self.separatorInset = UIEdgeInsetsMake(0, 10, 0, 10);
而且很方便,你可以自定义分隔符的位置,而不需要你的设计师为你画一个..........
我是这样做的:
tableView.separatorInset = UIEdgeInsetsZero;
tableView.layoutMargins = UIEdgeInsetsZero;
cell.layoutMargins = UIEdgeInsetsZero;