简体   繁体   中英

UITableView showing more rows than specified in numberOfRowsInSection:

I want my tableView to show 6 rows with text in it, in this case "Example." As far as I can tell, I have my numberOfSectionsInTableView: and numberOfRowsInSection: set properly. See example code below:

- (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;
}

The problem is when you see the image below showing lines for rows that shouldn't/don't exist.

在此输入图像描述

How do I get rid of the lines showing past row 6?

The generally accepted way of doing this is to add a footer view with a frame size of CGRectZero, as such:

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

What this does is tell the table that there is a footer, and so it stops displaying separator lines. However, since the footer has a CGRectZero as its frame, nothing gets displayed, and so the visual effect is that the separators simply stop.

Swift Version

The easiest method is to set the tableFooterView property:

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

This is Because of Your Table-view Height. Weather you have Write

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

// Return the number of rows in the section. return 6; }

But its show rows According to Table-view Size. If you Dont want to show This extra Lines then Make UITableView Style Plain To Grouped.

简短回答..

self.tableView.tableFooterView = [UIView new];

You could do something along the lines of:

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

Im sure there are some better ways but this is the first thing that came to mind.

If you're referring to the light gray lines that appear below the last row, that's simply the default way a UITableView draws the row separator.

You could try changing the Separator style in Interface Builder (see the images below) to see if one of those might be more to your liking.

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

You didn't say what you do want to see past the last row. If you just want to see the window background, then just embed your table view in a UIView that's just tall enough to show the number of rows you want to see. If you want to see more rows without scrolling, then you would have to adjust the size of that containing view based on the number of rows.

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

It's a lot easier to:

  1. return numberOfSections + 1
  2. return 0 rows in the final section

This keeps it simple!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM