显示比numberOfRowsInSection中指定更多的行的UITableView:

我希望我的tableView显示6行文本,在这种情况下,“示例”。 据我所知,我有我的numberOfSectionsInTableView:numberOfRowsInSection:设置正确。 看下面的示例代码:

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ // Return the number of sections. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ // Return the number of rows in the section. return 6; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{ static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } cell.textLabel.text = @"Example"; return cell; } 

问题是当你看到下面的图片显示行不应该/不存在的行。

在这里输入图像说明

我如何摆脱显示过去第6行的行?

这样做的普遍接受的方法是添加一个CGRectZero框架大小的页脚视图,如下所示:

 [tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]] 

这样做是告诉表有一个页脚,所以它停止显示分隔线。 然而,由于页脚有一个CGRectZero作为框架,所以什么也不显示,所以视觉效果就是分隔符停止。

Swift版本

最简单的方法是设置tableFooterView属性:

 override func viewDidLoad() { super.viewDidLoad() // This will remove extra separators from tableview self.tableView.tableFooterView = UIView(frame: CGRect.zero) } 

这是因为你的桌面高度。 你有写的天气

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 

//返回该节中的行数。 返回6; }

但其显示行根据表视图大小。 如果你不想显示这个额外的行然后使UITableView风格平原分组。

短而简单的答案..

 self.tableView.tableFooterView = [UIView new]; 

你可以做一些事情:

 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:7 inSection:0]; [self.mytableView cellForRowAtIndexPath:indexPath].hidden = YES; 

我确定有一些更好的方法,但这是首先想到的东西。

如果您所指的是最后一行下方的浅灰线,那么这只是UITableView绘制行分隔符的默认方式。

您可以尝试更改Interface Builder中的分隔符样式(请参阅下面的图像),以查看其中的一个可能是否更符合您的喜好。

在这里输入图像说明在这里输入图像说明

你没有说你想看到最后一行。 如果你只是想看到窗口背景,那么只需将你的表格视图embedded一个UIView中,该UIView的高度足以显示你想要查看的行数。 如果你想看到更多的行而不滚动,那么你将不得不根据行数调整包含视图的大小。

以编程方式删除它,使用这个: [yourTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];