Reputation: 2121
I have added a tableview
with 10 records. The last row is the load more records
cell. In appStore
when you search for an app, and if there's more apps to be shown, then the last row says load more records
(or something similar).
Now, if i have 10
records, and if the 11th cell says load more records, how do i program it to display 5
more records when the user clicks on the last row.
I have my 10
records stored to an array, so is it possible to append 5
more records to the array, without changing the order of it ?
Or do i have to load the whole array with 15 records, and then display it in the tableview
? (I don't think this is the best approach)
Hope i made my question clear.
Sorry, i don't have code to show my workings, i am stuck trying to figure this out.
Upvotes: 0
Views: 203
Reputation: 3222
Allow me to give a simple explanation on how tables work in iOS. Every table has 2 main components: the data to be displayed, and the view (the display) of the table. The data is the source of information of the table. Any kind of data source can be used as the data source of your table (static info like NSString
, arrays, mutable arrays, dictionaries...). The exact type of your data source depends on the application itself.
These data has to be viewed to the user and this is the role of the UITableView
. The code that is responsible for loading the data to the view is always inside the function tableView:cellForRowAtIndexPath:
. In this function you do the loading and you also specify some view properties like the type of cells, style of table...
Now the loading is taken exactly in the same order of the data source (unless you specify another way). So if your data is in an array, the first element of the array will be in the first cell of the table and so on.
Now to answer the first part of your question, what type should your data source be? Since you are going to add things to your data periodically then for sure you need a mutable type. This could be NSMutableArray
or NSMutableDictionary
. Use arrays when your source of information is only one item, for example you are only adding names of players, and use dictionaries when your information has more than one item per cell like the player name, player age, player score (and say you are willing to display all these info in the cell; also remember that you can customize the view of the cell and do custom cells). Well, why mutable types? Because they allow to add an remove items to them. How to add and where do they sit? Usually mutable types has addObject
which is used to add and the new item is always added to the end of the array.
I hope this explanation helps you understand how things work so you can imagine what solutions you need to do to solve your problem.
For a table to notice the change of the data source you always need to call:
[myTableView reloadData];
Reload data will force the function tableView:cellForRowAtIndexPath:
to be called again and to reload all the data found in your data source, and for sure in the order they are in that data source.
Upvotes: 3
Reputation: 2177
Just add the new records to the array like this
[array addObject:record];
and the recorded will be added at the end of the array. Use
[tableview reloadData];
to tell your table view that new data is available to be displayed.
Upvotes: 0