I'm trying to create the capability of deleting rows from a tableview and this is how I did it. The error is coming from the
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath] withRowAnimation:UITableViewRowAnimationFade];
Any help would be very much appreciated. Thank you!
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.homeworkArray removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
[self.tableView reloadData];
}
the arrayWithObjects: method requires a nil to terminate the list, so you need to say -
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationFade];
}
or even easier, use arrayWithObject:
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
Or as #rmaddy points out, use array literal syntax -
[self.tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
Also, you don't need the reloadData call - deleteRowsAtIndexPaths will take care of updating the relevant rows in the table view.
It might be Help full for you I know it Same As Another Answer But it is my Own answer :-
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[arrStations removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
Related
-(void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.myArray removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
}
Changing the animation type to any of the other options does not make a difference, I seem to get the same animation every time. I've read that this could be due to an iOS 7 bug, but my deployment target is iOS 8. Any ideas?
if (editingStyle == UITableViewCellEditingStyleDelete)
{
if ([self.myArray count] >= 1)
{
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.myArray removeObjectAtIndex:[indexPath row]];
[self.tableView endUpdates];
}
}
Try this
You can do like this way,
NSMutableArray *indexPathsToDelete = [NSMutableArray new];
for (Object *object in newObjects)
{
if (![currentObjects containsObject:object]) {
int row = [newObjects indexOfObject:object];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
[indexPathsToDelete addObject:indexPath];
}
}
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView endUpdates];
I'm trying to delete a UITableviewCell with a swipe gesture.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
With
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[_commentsData removeObjectAtIndex:indexPath.row];
_NBComment--;
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
}
The result is this:
The delete buttons remains displayed after the row deletion.
Do you have an idea to make the row deletion smoother?
Thanks in advance
Don't ask me why, but this code works:
[_commentsData removeObjectAtIndex:indexPath.row];
_NBComment--;
[tableView beginUpdates];
[tableView setEditing:NO animated:NO];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
You can try adding this code after deletion:
[tableview reloadData];
I'm working on an application where i need to expand collapse rows in different sections.
Initially I tried expanding collapsing rows with a single section.
Now I want the same to happen in multiple sections.
here is my code
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//Return the number of sections.
return [mysection count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSArray *rate=[mysection objectAtIndex:section];
return [myrow count];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (selectedSection==indexPath.section) {
if(selectedIndex==indexPath.row){
selectedSection=-1;
selectedIndex=-1;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
return;
}
//user taps on different row
if(selectedIndex!=indexPath.row){
NSIndexPath *prevPath=[NSIndexPath indexPathForRow:selectedIndex inSection:selectedSection];
selectedSection=indexPath.section;
selectedIndex=indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:prevPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
}
selectedIndex=indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
return;
}
else if(selectedSection!=indexPath.section){
selectedSection=indexPath.section;
[tableView reloadSections:[NSIndexSet indexSetWithIndex:selectedSection] withRowAnimation:UITableViewRowAnimationFade];
if(selectedIndex==indexPath.row){
selectedSection=-1;
selectedIndex=-1;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
return;
}
//user taps on different row
if(selectedIndex!=indexPath.row){
NSIndexPath *prevPath=[NSIndexPath indexPathForRow:selectedIndex inSection:selectedSection];
selectedSection=indexPath.section;
selectedIndex=indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:prevPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
}
selectedIndex=indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
selectedSection=indexPath.section;
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
NSLog(#"%d",selectedSection);
}
only my rows should be expanded not my sections
Please provide with some possible solutions.
thanks in advance
my table view has multiple sections and my section has many rows
tapping on row in that section should expand
In didSelectRowAtIndexPath i get the index path to a variable that can be accessed throughout the coding as below...
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
selectedCellIndexPath = indexPath;
[tableView reloadData];
}
in Heightforrowatindexpath
if(selectedCellIndexPath != nil && [selectedCellIndexPath compare:indexPath] ==NSOrderedSame)
{
return height;
}
Basically I have an app where you can swipe a tablecell off the screen and it "deletes" the cell. My delete method is called by this: (this happens when the cell reaches a certain point on the phone)
[tableCellTrash addObject: [NSIndexPath indexPathForRow:[numbers indexOfObject:[aPanGestureRecognizer view]] inSection:0]];
UITableViewCell *cell = [numbers objectAtIndex:[numbers indexOfObject:[aPanGestureRecognizer view]]];
[self deleteCell: cell];
This is the delete cell method
-(void)deleteCell: (UITableViewCell *) cell{
[self.tableView beginUpdates];
[numbers removeObject:cell];
[self.tableView deleteRowsAtIndexPaths:tableCellTrash withRowAnimation:UITableViewRowAnimationFade];
[tableCellTrash removeObjectAtIndex:0];
[self.tableView endUpdates];
//[self.tableView reloadData];
}
I have no idea what's wrong, the cell doesn't delete. Any help would be appreciated.
tableviews have a delegate method to handle the event for delete:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
[tableView reloadData];
}
I have a problem, I want to delete row in UITableView but don't use swipe button delete. I want call function removeCell after condition. My problem is I cannot delete first row in UITableView because I don't know get indexPath of row to delete.
If I swipe Delete button, it work perfect. But I want don't use Delete Button.
- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
deleteindexPath = indexPath;
/*[_tableView beginUpdates];
if (editingStyle == UITableViewCellEditingStyleDelete) {
[_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
[_tableView endUpdates];*/
[self removeCell];
}
removeCell():
-(void) removeCell
{
[_tableView beginUpdates];
[_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:deleteIndexPath] withRowAnimation:UITableViewRowAnimationFade];
// }
[_tableView endUpdates];
}
I try set deleteIndexPath = [_tableView indexPathForCell:cell]; in cellForRowAtIndexPath() but not work, crash.
Can you have suggestion?
Just try this
-(void) removeCell
{
[_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:deleteIndexPath] withRowAnimation:UITableViewRowAnimationFade];
//Now remove data from your tableArray also..
[_tableView reloadData];
}