Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

enter image description here

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

like image 240
tarheel Avatar asked May 27 '12 04:05

tarheel


People also ask

How do I populate UITableView?

There are two main base ways to populate a tableview. The more popular is through Interface Building, using a prototype cell UI object. The other is strictly through code when you don't need a prototype cell from Interface Builder.

What class does UITableView inherit from?

The appearance of the tableview is managed by UITableView class, which inherits UIScrollView. In tableview, the row is simulated by the object of the UITableViewCell class, which can be used to display the actual content. We can customize the tableview cells to display any content in the iOS application.


1 Answers

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.

like image 199
CrimsonDiego Avatar answered Oct 01 '22 03:10

CrimsonDiego