UITableView显示的行数多于numberOfRowsInSection中指定的行数:

tar*_*eel 31 iphone objective-c uitableview ios

我希望我的tableView显示6行,其中包含文本,在本例中为"Example".至于我可以告诉大家,我有我的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;
}
Run Code Online (Sandbox Code Playgroud)

问题是当您看到下面的图像显示不应该/不存在的行的行时.

在此输入图像描述

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

Cri*_*ego 57

普遍接受的方式这样做的是添加页脚视图与CGRectZero的帧大小,因为这样的:

[tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]]
Run Code Online (Sandbox Code Playgroud)

这样做是告诉表有一个页脚,所以它停止显示分隔线.但是,由于页脚有一个CGRectZero作为其框架,因此不会显示任何内容,因此视觉效果是分隔符只是停止.


Kin*_*ard 21

Swift版本

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

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

  • Swift 3:`= UIView(frame:CGRect.zero)` (2认同)

Wol*_*ine 8

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
Run Code Online (Sandbox Code Playgroud)

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

但它的显示行根据表视图大小.如果你不想显示这个额外的行然后使UITableView样式平原分组.


ios*_*ner 5

简短回答..

self.tableView.tableFooterView = [UIView new];
Run Code Online (Sandbox Code Playgroud)