32

我希望我的 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 行的线条?

4

9 回答 9

59

普遍接受的方法是添加一个帧大小为 CGRectZero 的页脚视图,如下所示:

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

这样做是告诉表格有一个页脚,因此它停止显示分隔线。然而,由于页脚有一个 CGRectZero 作为它的框架,所以没有显示任何内容,因此视觉效果是分隔符简单地停止。

于 2012-05-27T04:53:59.140 回答
24

斯威夫特版本

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

override func viewDidLoad() {
    super.viewDidLoad()
    // This will remove extra separators from tableview
    self.tableView.tableFooterView = UIView(frame: CGRect.zero)
}
于 2014-12-13T22:28:27.577 回答
8

这是因为您的表视图高度。你的天气写

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

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

但它根据表格视图大小显示行。如果您不想显示这些额外的行,则将UITableView 样式设为普通到分组。

于 2012-05-27T05:08:27.410 回答
5

简短而简单的答案..

self.tableView.tableFooterView = [UIView new];
于 2013-08-07T06:52:01.413 回答
0

您可以按照以下方式做一些事情:

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

我相信有一些更好的方法,但这是我想到的第一件事。

于 2012-05-27T04:22:55.423 回答
0

如果您指的是出现在最后一行下方的浅灰色线条,那只是 aUITableView绘制行分隔符的默认方式。

您可以尝试更改 Interface Builder 中的 Separator 样式(请参见下图),看看其中一个是否更符合您的喜好。

在此处输入图像描述 在此处输入图像描述

于 2012-05-27T04:41:31.697 回答
0

你没有说你想在最后一行看到什么。如果您只想查看窗口背景,则只需将表格视图嵌入到 UIView 中,该 UIView 的高度足以显示您想要查看的行数。如果您想在不滚动的情况下查看更多行,则必须根据行数调整包含视图的大小。

于 2012-05-27T04:55:37.417 回答
-1

要以编程方式删除它,请使用以下命令: [yourTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

于 2013-02-04T08:19:42.757 回答
-1

更容易:

  1. 返回 numberOfSections + 1
  2. 在最后一部分返回 0 行

这让它变得简单!

于 2018-06-24T14:41:30.670 回答