Objective c - Deleting rows of a table view?

"ON" : @"OFF"); // We are going to delete 2 rows numberOfRows -= 2 and in your numberOfRowsInSection instead of returning 9 each time, return the new number of rows (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return numberOfRows; }.

The number of rows in the table is in your - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section You need this to match the new number of rows in the table ---- EDIT This means that you will need to keep track of the number of rows in your table. Start with this set to 9 and delete 2 every time you change the switch - (IBAction)switchValueChanged:(id)sender { NSLog(@"%@", (switch isOn)? @"ON" : @"OFF"); // We are going to delete 2 rows numberOfRows -= 2; ... and in your numberOfRowsInSection instead of returning 9 each time, return the new number of rows - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return numberOfRows; }.

I have implemented this method already, setting it to return 9. Do I have to implement this method again? – ozking Oct 21 at 16:38 See my edit for more details.

Basically, you have to return the number of rows in the table after the delete - the table view will call this automatically for you. You're deleting two rows so you need to return 7 instead of 9. – deanWombourne Oct 21 at 17:10 I've succeeded, Thanks :) – ozking Oct 23 at 13:10.

You should delete the data from the datasource not the table cells and then just to refresh the table view. At least this is how I would do the design.

I don't have a data source, I have created the cells manually. I can't delete them because I have to insert them back later. – ozking Oct 21 at 16:42 Then you need two arrays of cells - one for the currently visible cells and one for the cells you have deleted.In your switchvaluechanged method move the two cells between the arrays.

When you implement numberOfRowsInSection don't return 9, return the number of items in the visible cells array. – deanWombourne Oct 21 at 17:14 or you can just put a flag on each of your objects: displayTheObject and then (as deanWombourne mentioned) return only the number of the objects without this flag. – alinoz Oct 21 at 17:43 You could have a flag but that might make your cellForRowAtIndex path more complicated - with two arrays you can just say return visibleCells objectAtIndex:indexPath.

Row :) – deanWombourne Oct 21 at 19:31.

You have to also delete the data from the datasource of the table view. The data source is any container that holds the actual data which is being displayed on the table view. Just deleting the row only deletes the visual item of the row, when tableview is reloaded - it will want to draw 9-2 rows....but since number of rows is still returning 9, it will throw an inconsistency exception and tell you why.

See the log carefully.

I've really searched a lot and yet find an answer to the next problem. I have a table view with 9 cells. One of the cells has a switch button in it.

When the switched button value changes, I want to delete 2 rows of the table view. When I run this, I'm getting a problem relating to the number of rows in the section - this number has been changed so I need to change it. I really can't find how I change it, but I know where I have to change it (see code).

How can I do it? How can I call the method numberOfRowsInSection:NSInteger and set also the rows?

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions