Tutorial :Static cell from Nib file is not shown


I have a Nib file containing grouped table view and a cell. The cell is connected to UITableViewController through outlet. In cellForRowAtIndexPath I just return that cell and I can see a single cell in the table view. But when I change the row count of table to 2 and want to show the same cell, then I can see only one, it appears that the second cell is intended to be there, as the lower corners of the visible cell are not rounded, however, it's not there.

If I create a second cell object in nib file, second outlet and return it as second row, the it appears fine. My cell has identifier specified in IB.

Does it mean I can't re-use cell object for more than one row?


In the same way as you would need one instance of a UIButton for each visible button in your view, you will need one instance of your cell for each visible row.

The common pattern to manage this, is to ask the tableview for a previously instantiated cell that is no longer needed (dequeueReusableCellWithIdentifier:), and then return that cell. If the table view does not have any reusable cells, you have to instantiate a new one from your nib file.

There are many examples on this around the web, and you can also find some here at SO, ex in this answer.

I would recommend that you read through Apples TableView Programming Guide, which also contains a section on loading cells from nibs.

UPDATE: An attempt on explaining the TableView and reuse of cells in a different way.

Lets say we have a large gallery with old paintings. Thousands of paintings. The gallery has just one display room, though, and it has walls for just ten paintings. The gallery manager has to switch paintings now and then when the visitors get bored and want to see some new paintings.

Every displayed painting needs a frame. Without a frame, it can't be put on a wall. Frames are expensive to make, and take up a lot of space. The frame maker guy want have time nor money to build the thousands of frames needed.

He finds out that he want be needing frames for all the paintings that is not shown at the moment. He would only need ten frames for the currently displayed paintings. When the gallery manager takes down a painting, the frame maker stores the frame, and when the gallery manager put up a new painting and asks the frame maker for a frame for it, the frame maker returns the frame from the previous painting again.

One day, the needed-space-between-paintings-regulations gets changed for no good reason. The gallery manager is able to put up two more pictures in the display room. He picks two paintings from the store room, and asks the frame maker for frames. The frame maker has no spare frames, and need to make two new frames.

Now, lets say that the gallery is a TableView, and all the paintings are rows of data. The display room with space for ten visible paintings, is the screen, with space for ten visible rows. Each visible row would need a cell, just like each displayed painting would need a frame.

In the end, you shouldn't care that much about saving resources by reusing one cell. That's TableViews responsibility. It's an implementation detail of the TableView how many cells is needed and how it is used. The protocol defines how you can ask the TableView for an reusable cell, and the documentations states that you should. That should be enough. Demo projects shows that TableView can manage very large amounts of data. If your projects struggles with performance because of instantiating 10-20 cells from nib, you probably got some problems with your nib file or something. There are some discussions, though, about the performance of loading from nib versus building cells in code. It may be interesting to you.


I had some very weird behavior that sounds very much like what you are describing some time ago.

Eventually I found that the problem was that I had just added a table view cell to a xib which contains other items such as the parent table view and controller. What I had to do was create a seperate xib for each table view cell individually. I think the issue was that loading the table view cell from an incorrectly built xib was confusing the issue.

As Vegar said there are a lot of tutorials on how to do it.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »