Table cells delete wrongly - iphone

I have an app set up to have a tableview, which allows the user to add and delete cells (which are events). Adding and deleting DOES work, however when I delete a cell (by entering edit mode), I can click the (-) button to delete, then I hit the delete button, however the delete button stays highlighted and the cell does not disappear until I hit the "Done" button which exits edit mode. Is this an issue anyone has seen? If so, is there a solution? Thanks
EDIT:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Delete");
[eventList removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject: indexPath] withRowAnimation:YES];
[tableView reloadData];

I suggest removing the call to reloadData in commitEditingStyle. The deleteRowsAtIndexPaths already takes care of redrawing the table view.
The best place to start looking for written sources is the documentation.
See "An Example of Deleting a Table-View Row" on this page.
Also note that in deleteRowsAtIndexPaths, withRowAnimation does NOT take a BOOL parameter. Instead of YES, it should be a UITableViewRowAnimation enum value like UITableViewRowAnimationFade.

You definitely don't need the reloadData after doing the row deletion. If it still doesn't work, you might want to try wrapping the deletion in a beginUpdates and endUpdates. Finally, the withRowAnimation: argument should be one of the UITableViewRowAnimation enums instead of a BOOL.

Related

UITableView doesn't animate deletion if multiple cells

I have my UITableView (swipe) deletion method set up in the standard fashion.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.tableView beginUpdates];
/*
code…
*/
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
}
}
The UITableViewRowAnimationFade animation works perfectly if there is only one cell in the table view or if I am deleting the last cell, but if I try to delete a row above the last one, it will no longer use the animation when I hit delete. How can I fix this?
Also, I am using a custom cell (just a label and an image view) if it makes any difference.
This is a bit late, but for anyone having this issue, I have created a test project that replicates the required functionality, with animation working perfectly for all cells.
It's available here: https://github.com/sdods3782/TVTTableViewTest
However, I was unable to recreate the issue. I don't know what was causing the problem for the original poster, but using TVTTableViewTest as an example, it should hopefully prevent other people from having the same problem.

How to delete a UITableView row when I press the delete button?

I have a UITableView. I would like a delete button to be shown when I slide (swipe) the table cell. Then, once I press the delete button, the row will be deleted.
How am I going to be able to do that? Does anyone have an idea about this iPhone feature?
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(editingStyle == UITableViewCellEditingStyleDelete)
{
[listArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
}
}
Simply remove the corresponding data from your model (probably an NSArray your are based on to fill your tableView), in your implementation of tableView:commitEditingStyle:forRowAtIndexPath: method. Then call [tableView reloadData] to ask the tableview to reload itself.
Note: You can also use row insertion/deletion animation features (see documentation about deleteRowsAtIndexPaths:animation:), BUT before trying to manage the row animation, you first need to know how to remove your model -- because they always have to be synchronized, and trying to play around with deleteRowsAtIndexPaths:animations:, without first manage to delete the row from your model successfully w/o animation, will surely make your app throw an InconsistencyException. So go one step after another, and start the basic step first, by modifying your model part of your MVC (your NSArray holding the data) before trying to go further!

Dynamically add and remove UITableViewCells to UITableView

I'm building an app where a user is able to provide different usernames that he/she has. The vision is, the user is able to add and remove UITableViewCell to enter a username.
Right now I have a grouped UITableView and on the right hand side of every UITableViewCell I have a UIButton that adds another cell to the table with a UITextField. After the first cell, every cell has a delete button. I'm trying to make the UIButton delete that row. I have the IBAction that removes the cell, the only problem is, it's not deleting the proper row.
What is the best way to do what I'm attempting to do? I don't know how to properly search for this on Google. I'm sure someone has done what I'm trying to do.
Thanks for any help in advance!
Similar to what Derek said above -- UITableViewController already provides functionality to delete rows.
To toggle editing a UITableView, do something like: [self.tableView setEditing:!self.tableView.editing animated:YES];
Override tableView:canEditRowAtIndexPath: with something like (since it sounds like you don't want your first row to be deletable):
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
if ([indexPath row] == 0) {
return NO;
}
return YES;
}
Also override tableView:commitEditingStyle:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.dataArray removeObjectAtIndex:[indexPath row] - 1];
// delete the row from the data source
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
}
}
Hope this helps!
It sounds like you are trying to code what a table view already has. Look into the built in editing facilities of a table view and you will find thaaat you don't need to have these unbuttons as table views and delegates have editwing facilities built in.
Your comment about deleting the wrong thing makes me think you have an issue with matching row numbers to indexes of your source data.

How do I add or remove an off-screen row at the top of a Table View without scrolling?

I have a UITableView containing a list of items.
When the user taps the Edit UIBarButtonItem, the top row needs to be removed, because it is not editable.
I do that like this:
- (void) setEditing: (BOOL) editing animated: (BOOL) animated
{
if (editing)
{
[self.theTableView deleteRowsAtIndexPaths: [NSArray arrayWithObject: [NSIndexPath indexPathForRow: 0 inSection: 0]] withRowAnimation: UITableViewRowAnimationTop];
// call super afterward so the user doesn't see the row get an editing accessory before it disappears
[super setEditing: editing animated: animated];
}
else
{
[super setEditing: editing animated: animated];
[self.theTableView insertRowsAtIndexPaths: [NSArray arrayWithObject: [NSIndexPath indexPathForRow: 0 inSection: 0]] withRowAnimation: UITableViewRowAnimationTop];
}
}
This code (unrelated bits snipped) works just fine, with one caveat:
If the user is anywhere but at the top of the table (assuming there is more than a single screenful of row), the animation behavior causes the entire table view to scroll up (or down), and the behavior of the last row in the table is, at best, usable.
My question: How can I only make the top row animate out when the user can see it, but when the user can't see it, it just disappears, but does not cause the table to scroll.
Have you tried UITableViewRowAnimationBottom instead of UITableViewRowAnimationTop?
Otherwise you can manually set the contentOffset property of the UITableView to adjust for the missing row.
Try using UITableViewRowAnimationRight. That should make the cell slide out if it's visible, but otherwise leave your table intact.
Before you do the animation check if the first row is visible.
If yes, peoceed.
Else, you want it not to appear when the user scrolls up, right? so replace the table's data source with one without the first row and after editing ends return the first row to that data source.
Another option might be not displaying it by manipulating
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
But that is probably a bad practice.
There doesn’t appear to be any simple solution to this. I’ve had limited success with completely custom behavior in a UICollectionView, but major versions of iOS and years later, even Apple’s own built-in apps continue to get this edge case wrong.

Deleting custom cell in Table View

I am using a Table View Cell to draw custom cell in table view. Now the problem is that when I try to delete the cell in editing style UITableViewCellEditingStyleDelete only the
the little red -ve sign bitton appear and when I click it nothing happen.
Please help me and tell me how to delete custom cell in table view
Thanks
Gyani its not at all possible that when you click on (-) sign nothing happens.
May be your tables user interaction is disabled. Try enabling tableView.userInteractionEnabled=YES
And still it does not work then post some code.
hAPPY cODING...
did you implement the appropriate delegate methods? Specifically
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
Did you implement this delegate method by following form?
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// remove the cell from UITableView
[tableView deleteRowsAtIndexPaths:indexPath withRowAnimation:YES];
// remove the data from self array or something else.
...
}
}
Do you have a subview in the cell that stops the user interaction? maybe located in negative x, so it is over the delete button.