UITableView分隔符样式问题 [英] UITableView Separator Style Question

查看:104
本文介绍了UITableView分隔符样式问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个默认为空白的tableview。用户可以添加单元格。

I have a tableview that is blank by default. User can add cells to it.

我希望没有单元格时分隔线清晰,有单元格时灰色。

I want the separator lines to be clear when there are no cells, and grey when there are cells.

我正在使用此代码:

if ([[self.fetchedResultsController fetchedObjects] count] == 0)
{
    self.routineTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.routineTableView.separatorColor = [UIColor clearColor];
}
else
{
    self.routineTableView.separatorColor = [UIColor grayColor];
}

问题是,当我启动带有空白表的应用程序时,如果我添加单元格,灰色线条不在那里,直到我重新启动应用程序。但是,如果我从那里的单元格开始,然后删除它们,然后重新添加它们,行就在那里。有什么建议吗?

The problem is, when I launch the app with a blank table, and if I add cells, the grey lines are not there there until I restart the app. But if I start with cells there, then delete them, then re-add them, the lines are there. Any suggestions?

推荐答案

也许你错过了这个?

...
else
{
    self.routineTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; // or you have the previous 'None' style...
    self.routineTableView.separatorColor = [UIColor grayColor];
}






编辑
你需要这个但不仅如此...根据Apple文档:


EDIT : You need this but not only this... According to Apple Documentation :


此属性的值是 UITableViewCell 类引用类引用中描述的分隔符样式常量之一。 UITableView 使用此属性在 tableView:cellForRowAtIndexPath:中的委托返回的单元格上设置分隔符样式。

The value of this property is one of the separator-style constants described in UITableViewCell Class Reference class reference. UITableView uses this property to set the separator style on the cell returned from the delegate in tableView:cellForRowAtIndexPath:.

这意味着对于已经加载的单元格,样式不会改变。只需滚动表格以强制细胞重绘就应该使分隔符显示...

That means the style wont change for cells that are already loaded. Just scrolling the table to force cells to redraw should make separators appearing...

然后你必须:


  1. 在插入单元格之前设置它

  1. set it BEFORE cell is inserted

OR

这对于一个不容易做到 NSFetchedResultsController ,您应该查看其委托获取解决方案...或更改方向,例如隐藏 tableView 直到你拥有结果可能......

which is not easy to do with a NSFetchedResultsController, you should look into its delegate for a solution... or change direction, like hiding the tableView until you have a result maybe...

编辑2 :您也可以简单地添加:

EDIT 2 : You can also simply add this :

[self.tableView reloadData];

但这是一个肮脏的解决方法,只会重新加载完整的tableView,失去了 NSFetchedResultsController ...

but that's a dirty workaround that will just reload full tableView, losing most benefits of NSFetchedResultsController...

这篇关于UITableView分隔符样式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆