我有一个应用程序,其中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分隔符边距?
当前回答
使用storyboard更改分隔符插入自定义左0和右0, 如果你想隐藏分隔符为None 见上图
其他回答
在Swift中,这有点烦人,因为layoutmargin是一个属性,所以你必须重写getter和setter。
override var layoutMargins: UIEdgeInsets {
get { return UIEdgeInsetsZero }
set(newVal) {}
}
这将有效地使layoutmargin为只读,这在我的情况下是好的。
添加这个片段,简单优雅的Swift适用于我在iOS8:)
// tableview single line
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.preservesSuperviewLayoutMargins = false
cell.layoutMargins = UIEdgeInsetsZero
}
使用Swift 2.2
创建UITableViewCell扩展
import UIKit
extension UITableViewCell {
func removeMargins() {
if self.respondsToSelector(Selector("setSeparatorInset:")) {
self.separatorInset = UIEdgeInsetsZero
}
if self.respondsToSelector(Selector("setPreservesSuperviewLayoutMargins:")) {
self.preservesSuperviewLayoutMargins = false
}
if self.respondsToSelector(Selector("setLayoutMargins:")) {
self.layoutMargins = UIEdgeInsetsZero
}
}
}
现在可以在cellForRowAtIndex中使用
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.removeMargins()//To remove seprator inset
}
对我来说,简单的线条就够了
cell.layoutMargins = 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];