【发布时间】:2010-11-25 02:02:26
【问题描述】:
【问题讨论】:
标签: ios uitableview cocoa-touch
【问题讨论】:
标签: ios uitableview cocoa-touch
这是不使用分组表格样式的另一种方法,您可能猜不到。将 页眉和 页脚添加到表 (可能一个或另一个就足够了,尚未检查) 会导致分隔符从填充/空白行中消失。
我偶然发现了这一点,因为我希望在表格的顶部和底部留出一点空间,以减少按下按钮的风险,而不是手指多肉的表格单元格。这是一种将空白视图作为页眉和页脚粘贴的方法。使用任何你喜欢的高度,你仍然可以消除额外的分隔线。
- (void) addHeaderAndFooter
{
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 10)];
v.backgroundColor = [UIColor clearColor];
[self.myTableView setTableHeaderView:v];
[self.myTableView setTableFooterView:v];
[v release];
}
为了响应@Casebash,我回到了我的应用程序中的代码(iTunes 商店中的“AcmeLists”列表管理器......)并短路了 addHeaderAndFooter 方法来验证。 没有它,我有额外的行分隔符;使用代码,我有您在此窗口快照中看到的内容:no table row separators picture。所以我不确定为什么它对你不起作用。此外,对我来说,在表格视图上拥有任何自定义页脚都必须停止为其下方的空白行绘制行分隔符。那将是可怕的。作为参考,我查看了行数多于屏幕上无法查看的表格,然后查看了有两行的表格。在这两种情况下,都没有多余的分隔符。
也许您的自定义视图实际上并未添加。要检查这一点,请将背景颜色设置为 clearColor 以外的其他颜色,例如 [UIColor redColor]。如果您在表格底部没有看到一些红条,则说明您的页脚没有设置。
【讨论】:
我使用表格视图来显示固定数量的固定高度行,所以我只是调整了它的大小并使其不可滚动。
【讨论】:
只需将 UIView 拖到表格中即可。在情节提要中,它将位于自定义单元格下方的顶部。您可能更愿意将其命名为“页脚”。
为了清楚起见,这里显示为绿色,您可能需要清晰的颜色。
请注意,通过调整高度,您可以根据自己的喜好影响桌子“底部弹跳”的处理方式。 (高度为零通常很好)。
以编程方式进行:
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.tableFooterView = UIView()
}
- (void)viewDidLoad
{
[super viewDidLoad];
// This will remove extra separators from tableview
self.tableView.tableFooterView = [UIView new];
}
或者,如果您愿意,
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
添加到表格视图控制器...
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
// This will create a "invisible" footer
return CGFLOAT_MIN;
}
如果有必要...
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
return [UIView new];
// If you are not using ARC:
// return [[UIView new] autorelease];
}
【讨论】:
tableFooterView 会在行中的最后一个单元格上留下一个分隔符,因为表格仍会预测该单元格下方的内容。使用历史方法将消除最后一个分隔符,整体看起来更好。
我想延长 wkw 答案:
只需添加高度为 0 的页脚即可。 (在 sdk 4.2、4.4.1 上测试)
- (void) addFooter
{
UIView *v = [[UIView alloc] initWithFrame:CGRectZero];
[self.myTableView setTableFooterView:v];
}
甚至更简单 - 在您设置 tableview 的地方,添加以下行:
//change height value if extra space is needed at the bottom.
[_tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectMake(0,0,0,0)]];
甚至更简单 - 简单地删除任何分隔符:
[_tableView setTableFooterView:[UIView new]];
再次感谢wkw :)
【讨论】:
只需在 tableViewCell 的位置 x0 y-1 处添加一个具有所需分隔符颜色作为背景颜色、100% 宽度、1px 高度的视图。确保 tableViewCell 不剪辑子视图,而 tableView 应该。
因此,您只能在现有单元格之间获得绝对简单且有效的分隔符,而无需针对每个代码或 IB 进行任何破解。
注意:在垂直顶部反弹时,会显示第一个分隔符,但这应该不是问题,因为它是默认的 iOS 行为。
【讨论】:
试试这个。它对我有用:
- (void) viewDidLoad
{
[super viewDidLoad];
// Without ARC
//self.tableView.tableFooterView = [[[UIView alloc] init] autorelease];
// With ARC, tried on Xcode 5
self.tableView.tableFooterView = [UIView new];
}
【讨论】:
我知道这个问题已被接受,但我在这里提出了不同的方法来隐藏UITableView 的额外分隔线。
您可以隐藏tableView 的标准分隔线,并在每个单元格的顶部添加您的自定义行。
更新:
添加自定义分隔符最简单的方法是添加简单的UIView 1px 高度:
UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)];
separatorLineView.backgroundColor = [UIColor grayColor]; /// may be here is clearColor;
[cell.contentView addSubview:separatorLineView];
或
self.tblView=[[UITableView alloc] initWithFrame:CGRectMake(0,0,320,370) style:UITableViewStylePlain];
self.tblView.delegate=self;
self.tblView.dataSource=self;
[self.view addSubview:self.tblView];
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 10)];
v.backgroundColor = [UIColor clearColor];
[self.tblView setTableHeaderView:v];
[self.tblView setTableFooterView:v];
[v release];
或
- (float)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
// This will create a "invisible" footer
return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
// To "clear" the footer view
return [[UIView new] autorelease];
}
或 我喜欢的最好最简单的方法是
self.tableView.tableFooterView = [[UIView alloc] init];
尝试任何一种;
【讨论】:
只需将UIView 拖入您的UITableView 作为页脚即可。将页脚视图的高度设置为 0。
【讨论】:
在 Swift
中删除 UITableView 中空行的额外分隔线override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.yourTableview.tableFooterView = UIView()
}
【讨论】:
推进 J. Costa 的解决方案:您可以通过输入这行代码来对表进行全局更改:
[[UITableView appearance] setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
在第一个可能的方法中(通常在AppDelegate,在:application:didFinishLaunchingWithOptions: 方法中)。
【讨论】:
试试这个
self.tables.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 10.0f)];
【讨论】:
如果您使用的是 Swift,请将以下代码添加到管理 tableview 的控制器的 viewDidLoad 中:
override func viewDidLoad() {
super.viewDidLoad()
//...
// Remove extra separators
tableView.tableFooterView = UIView()
}
【讨论】:
override func viewDidLoad() {
super.viewDidLoad()
tableView.tableFooterView = UIView()
}
【讨论】:
如果你对 UITableView 进行子类化,你需要这样做......
-(void)didMoveToSuperview {
[super didMoveToSuperview];
self.tableFooterView = [UIView new];
}
【讨论】:
init(frame:style:)+init?(coder:) 中执行相同的操作,以保证只执行一次。
我很幸运地实现了一个已接受的答案(iOS 9+,Swift 2.2)。我曾尝试实施:
self.tableView.tableFooterView = UIView(frame: .zero)
但是,我的 tableView 没有任何影响 - 我相信这可能与我使用 UITableViewController 的事实有关。
相反,我只需要重写viewForFooterInSection 方法(我没有在其他地方设置tableFooterView):
override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return UIView(frame: .zero)
}
这适用于具有单个部分的tableView(如果您有多个部分,则需要指定最后一个部分)。
【讨论】:
如果您的view 中有searchbar(例如限制结果数量),您还必须在shouldReloadTableForSearchString 和shouldReloadTableForSearchScope: 中添加以下内容
controller.searchResultsTable.footerView = [ [ UIView alloc ] initWithFrame:CGRectZero ];
【讨论】:
uitableview 额外的分隔线隐藏额外的分隔线隐藏在 swift 3.0 中
self.tbltableView.tableFooterView = UIView(frame: .zero)
【讨论】:
Swift 适用于:
tableView.tableFooterView = UIView()
【讨论】:
我只是在 ViewDidLoad 函数中添加了这一行并修复了问题。
tableView.tableFooterView = [[UIView alloc] init];
【讨论】:
如果您不想在最后一个单元格之后使用任何分隔符,那么您的页脚需要一个接近零但非零的高度。
在你的UITableViewDelegate:
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return .leastNormalMagnitude
}
【讨论】:
您可以在最后添加一个空页脚,然后它会隐藏空单元格,但它也会看起来很丑:
tableView.tableFooterView = UIView()
有一个更好的方法:在表格视图的末尾添加一条 1 点线作为页脚,空单元格也将不再显示。
let footerView = UIView()
footerView.frame = CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 1)
footerView.backgroundColor = tableView.separatorColor
tableView.tableFooterView = footerView
【讨论】:
如果你想删除 UITableview 中不需要的空间,你可以使用以下两种方法
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 0.1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 0.1;
}
【讨论】:
要以编程方式从 UItableview 底部删除多余的分隔线,只需写下以下两行代码,它将从中删除多余的分隔线。
tableView.sectionFooterHeight = 0.f;
tableView.sectionHeaderHeight = 0.f;
这个技巧一直对我有用,你自己试试吧。
【讨论】:
只需添加此代码 (Swift) 。 .
tableView.tableFooterView = UIView()
【讨论】:
当tableView 具有tableFooterView 时,UIKit 不会创建空单元格。所以我们可以制作一个技巧,并指定一个高度为零的UIView 对象作为tableView 的页脚。
tableView.tableFooterView = UIView()
【讨论】:
【讨论】:
super.viewDidLoad())并且包含在顶部的 wiki 答案中。至于删除所有分隔符,这真的不是这个问题的目的,所以最好回滚你的最后一次编辑和/或完全删除你的答案。
Swift 3 /Swift 4 /Swift 5 +,非常简单的方法
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
//MARK:- For Hiding the extra lines in table view.
tableView?.tableFooterView = UIView()
}
或
override func viewDidLoad(_ animated: Bool) {
super.viewDidLoad(animated)
//MARK:- For Hiding the extra lines in table view.
tableView?.tableFooterView = UIView()
}
【讨论】:
viewDidLoad(_ animated: Bool) 不存在,每次视图出现时设置页脚也没用。抱歉,您的回答完全错误,不会为现有答案增加任何价值。
在 Swift(我使用的是 4.0)中,您可以通过创建自定义 UITableViewCell 类和 overriding setSelected 方法来完成此操作。然后将separator insets 全部设为0。 (我的带有表格视图的主类具有清晰的背景)颜色。
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// eliminate extra separators below UITableView
self.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}
【讨论】: