Initially i have a table view with only one add button.
when user press this button i need to increment the cells count and add the cells as follows
How to write row count and how to add new rows by click on the add button
//Row count
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return **????** ;
}
// Content on cells/rows
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
??????
}
// ### Add New Row.. ###
-(IBAction)myAction:(id)sender
{
???????? ;
}
Thanks in advance...
UITableView
has property to insert row or section. see Apple DocThere are many tutorial on this. there are 2 commonly use to add/delete row/section.
I have posted similar answer how to use this : Hiding the table view cells with switch ON/OFF in iPhone
In your application You have to add values to Array when you performing action in btn.
For example in your tableview you are displaying NSString in cell.textLABEL.text. These strings are in NSMutableArray.
Now when buttonAction is called
in myAction
Try this logic in your application regarding to your modal.
I hope this logic will be helpful to you.
Reloading of the table view each time you want to add or remove row creates poor experience for the user of your application. Despite the fact it isn’t efficient way to perform this task it also has some negative side effects - selected rows don’t stay selected after reload and change isn’t animated.
UITableView
has methods which were created to change content of the table view dynamically. These are:Notice that these methods allow you to specify kind of animation which will be used when specified operation is performed - you cannot achieve this kind of behavior when you use
reloadData
to modify the content of the table view.Moreover, you can even combine multiple table view operations using additional methods of the table view (this is not necessary):
beginUpdates endUpdates
Just wrap operations you want to perform into calls to
beginUpdates
andendUpdates
methods and table view will create one animation for all operations that have been requested betweenbeginUpdates
andendUpdates
calls so that whole transition looks nicer that one created by a few separated animations.It is really important to keep you data source state consistent with the one kept by
UITableView
. For this reason you must assure that when table view starts to perform requested operations its data source will return correct values.When table view start performing operations? This depends on whether operations are being in animation block defined by
beginUpdates
andendUpdates
methods. If yes, table view starts to perform operations afterendUpdates
method call. Otherwise, table view performs operations just after call to insert/move or delete method has been made.When you are using
beginUpdates
andendUpdates
methods to perform operations on table view you have to know that in this case table view ‚batches’ requested operations and performs them in specific order which is not necessary the same as order of the calls you made on your table view object (Apple's documentation on this topic).The most important thing to remember is that deletion all operations are always performed before all insertion operations. Also, deletion operations seems to be performed in descending order (operations for indexes 3, 2, 1) when insertion operation are performed in ascending order (operations for indexes 1, 2, 3). Remember about this is crucial in keeping state of your data source consistent with the one kept by table view.
Spend some time on analyzing order of the operations on data source and table view in presented in example below.
Final example:
You can just add object in array and reload your tableview on button click.
There are other correct answers here (which you should read because they go further in-depth), but I struggled for over a week with this after reading all the solutions I could find because there weren't any (that I found!) with comprehensive examples.
Rules for this to work: 1. Changes must be made directly to the array that contains the items you're displaying in the
UITableView
. If you set some value in aUITableViewCell
in thetableView:cellForRowAtIndexPath:
method to equal values inself.expandableArray
, then changes must also be made toself.expandableArray
in order for these methods to work.[tableView beginUpdates]
and[tableView endUpdates]
here is a very simple example that would work on its own.
I hope this saves someone a lot of time and headache. If you do it this way, you don't need to reload the whole tableView to update it, and you're able to select an animation for it. Free code, don't knock it.