jerik
jerik

Reputation: 5767

IB UITableView not defined prototype cells are displayed?

In Interface Builder in a UITableViewController I specified the amount of prototype cells and configred them. In the Code the numberOsSectionsInTableView: and tableView:numberOfRowsInSection: is set accordingly. In this case 5 cells.

Somehow in the Simulator more (empty) prototype cells are displayed.

enter image description here

Interesstingly I have another project where I did the same, but there the correct amount of prototype cells are show.

enter image description here

How do I fix this and just show the wanted amount of prototpye cells?j

Upvotes: 1

Views: 106

Answers (2)

Simon McLoughlin
Simon McLoughlin

Reputation: 8465

I realise this has already been answered but I feel the end to actually explain what is going on here rather than barking out some code.

First thing's first, prototype cells have no baring on how many cells are going to be displayed. When you set the number of static cells, thats how many will show up, dynamic cells are blank slates. You are suppose to use these to create 1 dynamic cell with a place holder for an image and a place holder for a piece of text. Then you tell the tableview how many rows you want and you get a reference to a reusable cell by calling the line:

[tblView dequeueReusableCellWithIdentifier:@" <id> "];

Then you would set the image and the text appropriately on the returned cell.

What you are doing is creating X "Blank slates" with default values and not overwriting these values. The way I have mentioned was designed for performance reasons and memory usage reasons. The way I mentioned effectively has 1 class instance in memory being reused, while yours has X many in memory all being used once.

Secondly the reason that you were seeing blank rows is that in a tableview not using grouped style, the tableView Will display as many rows as fit inside the bounds you have given it. Regardless of how many you tell it to display.

The reason this code:

self.tableView.tableFooterView = [[UIView alloc] init];

removes the blank rows is because a footer will be added after the last row with content, and the tableView will not display any rows, blank or otherwise after this point.

Hope this helps.

Upvotes: 2

GoGreen
GoGreen

Reputation: 2251

Try this code in your viewDidLoad:

- (void)viewDidLoad
{
  [super viewDidLoad];
 *// self.tableView.tableFooterView = [[[UIView alloc] init] autorelease];*   
  *//Since memory management is controlled by ARC now, so ignore autorelease*
  self.tableView.tableFooterView = [[UIView alloc] init] ;
}

Hope this helps!

Upvotes: 1

Related Questions