我有一个应用程序,其中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分隔符边距?
当前回答
以上的解决方案我都没有什么运气。我正在为我的表格单元格使用NIB文件。我通过添加一个高度为1的标签来“修复”这个问题。我把标签的背景改为黑色,把标签钉在笔尖的底部,然后把剩下的内容钉在添加的标签上。现在我在单元格的底部有了一条黑色边框。
对我来说,这感觉更像是一种黑客,但它确实有效。
我唯一的选择就是完全消除边界。我还没决定要不要这么做。
其他回答
至于cdstamper建议的表格视图,在单元格的layoutSubview方法中添加以下行对我来说是可行的。
- (void)layoutSubviews
{
[super layoutSubviews];
if ([self respondsToSelector:@selector(setSeparatorInset:)])
[self setSeparatorInset:UIEdgeInsetsZero];
if ([self respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)])
{
[self setPreservesSuperviewLayoutMargins:NO];;
}
if ([self respondsToSelector:@selector(setLayoutMargins:)])
{
[self setLayoutMargins:UIEdgeInsetsZero];
}
}
迅速:
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
}
简单地把这两行放在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);
只需重写cellForRowAtIndexPath并设置cell。preservessuperviewlayoutmargin = false" and "cell。separatorInset = UIEdgeInsets。零”和“单元格”。layoutmargin = uiedgeinsets . 0 "
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: LayoutTableViewCell! = tableView.dequeueReusableCell(withIdentifier: "LayoutCell") as? LayoutTableViewCell
let currentLayout = OrderLayouts()[indexPath.row]
cell.NameLabel?.text = currentLayout.name
cell.DescrLabel?.text = currentLayout.descr
if(GlobalVariables.debug){
cell.DescrLabel?.text = "\(currentLayout.id) \n \(currentLayout.descr)"
}
cell.preservesSuperviewLayoutMargins = false
cell.separatorInset = UIEdgeInsets.zero
cell.layoutMargins = UIEdgeInsets.zero
return cell
}
这在我的iOS 8和iOS 9系统中运行得非常好。
对于OBJ-C
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(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];
}
return cell;
}