Swift中的Tableview总是显示分隔线

8
我似乎在其中一个表视图中遇到了奇怪的问题,无法隐藏分隔线(我认为它们是分隔线,但它们位于屏幕中央,而不是紧贴右侧)。所有东西都是通过程序创建的,所以这里没有故事板问题。您可以看到以下每个单元格上方的微小1像素线。我使用以下方式加载我的表:
    self.tableView.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height)
    self.tableView.dataSource = self
    self.tableView.delegate = self
    self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
    self.tableView.backgroundColor = UIColor.whiteColor()
    self.tableView.scrollEnabled = true
    self.tableView.bounces = false
    self.tableView.separatorStyle = UITableViewCellSeparatorStyle.None
    self.view.addSubview(self.tableView)

我还有一个自定义标头,我使用以下代码实现:
```html

我也有一个自定义标题,我使用以下代码实现:

```
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {

    let header:UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView

    header.textLabel.textColor = UIColor.whiteColor()
    header.textLabel.frame = header.bounds
    header.textLabel.textAlignment = NSTextAlignment.Center

    view.tintColor = constants.getTintColor()
    header.textLabel.textColor = UIColor.whiteColor()

}

我尝试在不使用willDisplayHeaderView的情况下加载表格,问题仍然存在。
我还尝试添加...(后面的内容未给出,无法翻译)
tableview.separatorStyle = UITableViewCellSeparatorStyle.None 

并且。
tableView.separatorColor = UIColor.clearColor()

以下是实现此目的的方法:

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

    tableView.separatorStyle = UITableViewCellSeparatorStyle.None

}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    tableView.separatorStyle = UITableViewCellSeparatorStyle.None

    return self.boards.count
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    tableView.separatorStyle = UITableViewCellSeparatorStyle.None

    return self.boards[section].items.count
}

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?
{
    tableView.separatorStyle = UITableViewCellSeparatorStyle.None

    return self.boards[section].name
}

编辑:

这些单元格是标准的UITableViewCells,单元格的颜色是交替设置的,通过以下方式实现:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{

    let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell

    cell.selectionStyle = UITableViewCellSelectionStyle.None

    if indexPath.row % 2 == 0 {
        // Even
        cell.backgroundColor = constants.UIColorFromRGB(0x1EACE0)
    } else {
        // Odd
        cell.backgroundColor = constants.UIColorFromRGB(0x6FBEE5)
    }

    cell.textLabel?.textColor = UIColor.whiteColor()
    cell.textLabel?.text = self.boards[indexPath.section].items[indexPath.row].title

    return cell
}

编辑2:

我现在已经添加了分隔线,但这些不是分隔线,因为你仍然可以在分隔线下面看到它们。

enter image description here

求助!我很困惑。我有许多其他设置相同(就我所知)的表视图,它们都运行良好。

谢谢 Stack Overflow!


我们看到的不是默认的色调颜色吗?作为测试,还可以尝试设置contentView的背景颜色:cell.contentView.backgroundColor = ... - Matteo Piombo
@bwash70,你能给我展示一下你在xib中使用的自定义单元格和布局吗?如果你没有任何顾虑的话。 - Jatin Patel - JP
这不是自定义单元格。它是一个标准的UITableViewCell,在cellForRowAtIndex中设置了蓝色背景。 - bwash70
1
为什么要使用 let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell? 当您的单元格不是自定义单元格?同时,self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell") 是不必要的。尝试这个:let cell : UITableViewCell = UITableViewCell(style:UITableViewCellStyle.Subtitle, reuseIdentifier:"cell") - Amit89
你复制并粘贴了尽可能多的代码,但仍然无法重现你的问题 :-/ 你设置了rowHeight或其他什么吗? - Matteo Piombo
显示剩余14条评论
5个回答

7
在您的viewDidLoad()函数中,添加以下内容:
tableView.tableFooterView = UIView()

谢谢Danielle。这对我很有帮助......我个人认为,苹果应该将其作为默认设置添加进去。大多数情况下,我们会设置表视图的特定高度,如果没有数据可用,则会显示空白分隔线,看起来很奇怪。 - Ameer

7
你可以使用表视图属性 seperatorStyle 并将该属性设置为 UITableViewCellSeparatorStyleNone,或者从 StoryBoard 中删除分隔符。

enter image description here

将分隔符的属性设置为无

如果您正在使用页眉和页脚,则分隔线应为零,但可能会有额外的分隔符或页眉和页脚,请参考此链接消除UITableView下方的额外分隔符


嘿 - 我没有使用故事板,并已尝试使用tableview.separatorStyle = UITableViewCellSeparatorStyle.None。我看过你发布的链接,那是关于在表格视图中填充单元格下方出现额外线条的问题 - 而这并不是我的情况。 - bwash70

4

如果您没有使用自定义单元格,您需要执行以下操作。

创建单元格:let cell : UITableViewCell = UITableViewCell(style:UITableViewCellStyle.Subtitle, reuseIdentifier:"cell")

删除:self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")


1
这里有一些关于消除虚假分隔符的额外信息可能会有帮助。原作者提供的解决方案是创建一个新单元格:
let cell : UITableViewCell = UITableViewCell(style:UITableViewCellStyle.Subtitle, reuseIdentifier:"cell")

但是这位创作者并没有说明他是如何将这个方法整合到他的代码中去的,他的原始版本使用的是两个参数版本的dequeueReusableCellWithIdentifier:。
let cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell

我遇到了相同的问题,即出现虚假的分隔符,发现使用单参数版本的dequeueReusableCellWithIdentifier也可以解决这个问题:

let cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell

0

最近我遇到了同样的问题,最后在cell的init方法中找到了解决办法:

self.contentView.backgroundColor = UIColor.AppBgColor;
self.backgroundColor = UIColor.AppBgColor;

如果您仔细查看视图层次结构,您会发现单元格的背景和其contentView的背景不同,这导致了此问题。

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接