如何删除静态UITableView中只有一个部分的分隔符?

时间:2015-08-06 08:25:03

标签: ios swift uitableview static

我正在实现一个静态TableView,它在Swift中有两个部分。我希望仅在第0部分中与UITableViewCellSeparatorStyle.None具有相同的效果,而在另一部分中保留分隔符。由于它是一个静态TableView,我无法在cellForRowAtIndexPath函数中配置单元格(它总是得到一个单元格值nil)。我尝试为每个静态单元格制作一些IBOutlet并配置它们的seperatorInsetlayoutMargins属性,但它不起作用。所以我想知道是否有其他方法可以删除分隔符?谢谢!

4 个答案:

答案 0 :(得分:6)

首先添加效果UITableViewCellSeparatorStyle.None到tableView。之后,您可以在单元格中添加一个看起来像分隔符的小视图。 然后使用cell.in提供与分隔符View的连接。在cellForRowAtIndexPath中你唯一需要做的就是

if(indexPath.section == 0) {
     seperatorView.hidden = false
} else {
     seperatorView.hidden = true
}

这是解决问题的一个小技巧。希望它有所帮助

答案 1 :(得分:1)

使用tableview Separator没有直接的方法来设置它。正如你所说的静态表视图,这么简单的方法就是从tableview中删除Separator并在底部添加Separator UIComponent,它看起来像是所需单元格的Separator。

以下是enter image description here

的屏幕截图

答案 2 :(得分:1)

GetCell(Xamarin.iOS)中扩展@ chakshu的回答:

public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
    var customCell = TableView.DequeueReusableCell(SomeCellId, indexPath) as SomeCustomCell;
    if ( customCell == null )
        return null;

    var view = new UIView();
    var bounds = customCell.Bounds;
    view.BackgroundColor = UIColor.LightGray;
    view.Frame = new CoreGraphics.CGRect(bounds.X, bounds.Height - 1, bounds.Width, 1);
    customCell.Add(view);

    return customCell;
}

答案 3 :(得分:0)

1.首先将您的 tableViewSeparatorInset 设置为零:

tableView.separatorInset = .zero

2.验证separatorCell将出现在“cellForRowAt”中的位置,例如:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCell(withClass: DeliveryCell.self, for: indexPath)

    if indexPath.section == .deliverySection {
       cell.separatorInset = UIEdgeInsets(horizontal: tableView.bounds.width, vertical: 0)
    }
}
相关问题