delete UITableViewCell,depend on what section it is in - ios

I got a problem: I have a 2 sections UITableView,i want the cells in section one can be delete(when swipe left show the delete),and the cells in section two cannot be delete.
somebody give me a hint ,thanks.

You can implement the tableView:canEditRowAtIndexPath: method from the UITableViewDataSource protocol to determine whether the row can be deleted or not...
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 1) {
return NO;
} else {
return YES;
}
}
Then, to handle the delete you will use the tableView:commitEditingStyle:forRowAtIndexPath: method...
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
// Check editing style and take action (delete or edit)
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSLog(#"Delete row %ld in section %ld", (long)indexPath.row, (long)indexPath.section);
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
}
}
I'm assuming you have already implemented the UITableViewDataSource delegate protocol, so it should only be a matter of adding these methods.

Related

ios 9 UITableView freezes when deleting cell and does not show confirmation title

I have a code written in Objective-C for deleting cells from tableview.
Here's the code:
- (BOOL)tableView:(UITableView *)tableView
canEditRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == (self.numberOfAddsShowing - 1) && (*moreAvailable)) {
return NO;
}
if (isEditable) {
return YES;
}
return NO;
}
- (NSString *)tableView:(UITableView *)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
return #"حذف";
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
// If row is deleted, remove it from the list.
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSInteger rowNumber = indexPath.row;
if (rowNumber >= self.addsToShow.count) {
return;
}
HJAdd *adToDelete = [self.addsToShow objectAtIndex:rowNumber];
if (!self.adsToDelete) {
self.adsToDelete = [NSMutableArray array];
}
[self.adsToDelete
addObject:[[NSNumber numberWithInteger:adToDelete.addId] stringValue]];
[self.addsToShow removeObjectAtIndex:rowNumber];
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:#[ indexPath ]
withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
}
[self setEditButtonState];
}
The problem is that when I enter edit mode and try deleting the cell, the confirmation title is not shown and I am unable to delete the cell. When I finish editing mode, the UITableView is no longer scrollable. Swipe to delete is also not working. When I swipe, the tableView enters edit mode but I cannot see the delete button.
When I run the app in Simulator with iOS 8.1 or lower, its working fine.
Here's a video I uploaded to Youtube to show the problem.
Any help would be appreciated. Thanks to all!
You don't need [tableView beginUpdates]; and [tableView endUpdates];.
I think these are for code outside of the commitEditingStyle method.
In the TableView Programming Guide these are not used.
if you have a button bar Item
ViewDidload
self.navigationItem.rightBarButtonItem = self.editButtonItem;
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
TableAccountViewCell *accountCell = [tableView cellForRowAtIndexPath:indexPath];
if ([accountCell.myLabel.text isEqualToString:ADD_USER])
{
return NO;
}
// Return YES if you want the specified item to be editable.
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[menuItems removeObjectAtIndex:indexPath.row];
[_myTableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
//
// Start SQL Delele Account.
//
}
}

UITableView showing delete "-" dash on swipe to delete for cells out of view in other sections

I am encountering what seems like a bug with swipe to delete for a UITableView, when I swipe to delete in a visible section it shows the red delete dash in some rows for sections out of view. This happens in IOS SDK 8.2.
Sorry, tried to post an image, but I don't seem to have enough reputation to post an image.
Please provide any thoughts on what could be causing this, Thanks.
View Image http://i.imgur.com/xSdvdJH.png
Edited
Code used is Standard UITableView Delegates and Datasource Delegates to enable Swipe to Delete.
- (BOOL)tableView:(UITableView *)tableview shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
if ((self.sectionForCellSwiped == indexPath.section) && self.rowForCellSwiped == indexPath.row) {
return UITableViewCellEditingStyleDelete;
}else{
return UITableViewCellEditingStyleNone;
}
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.editEnabled) {
return YES;
}else{
return NO;
}
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
if ([self.DataSource numberOfForCategoryAtIndex:indexPath.section] == 1) {
[self.DataSource removeItem:removedItem];
[self.tableView beginUpdates];
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section]
withRowAnimation:UITableViewRowAnimationFade];
[self.tableView reloadData];
[self.tableView endUpdates];
}else{
[self.DataSource removeItem:removedItem];
[self.tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView reloadData];
}
}
[self.tableView reloadData];
[self.tableView setNeedsLayout];
[self.tableView layoutIfNeeded];
}

Custom UITableViewCell is not showing indent while editing

I'm on iOS 7, have an UITableViewController, with a UITableView (linked up the data source and delegate) and trying to use editing. I use a custom UITableViewCell, with Dynamice Prototype in UIStoryboard. When I click on the edit button (self.editButtonItem), you see the name change to done, but the are no indent icons (- icon). Swiping from the cell works, I get a delete button.
Here is my code:
self.navigationItem.leftBarButtonItem = self.editButtonItem;
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
All of my content is in the contentView
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[arr removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationBottom];
}
}
- (IBAction)editbtn:(id)sender {
if (self.tableView.editing) {
self.tableView.editing = NO;
[edit setTitle:#"Edit" forState:UIControlStateNormal];
}
else{
self.tableView.editing = YES;
[edit setTitle:#"Done" forState:UIControlStateNormal];
}
}
I think this code help you.
In a UIViewController self.editButtonItem doesn't work, you need to create a custom button.

Swipe delete row issue for UITableview

This is gonna drive me crazy! Lots of similar questions, but I still cant seem to figure out why this swipe-to-delete code of mine doesn't work! The 'Delete' button just doesn't appear. Is there some special way of swiping in the UITableview cell, in the simulator, compared to the device?
This is the code,
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
-(void) setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:animated];
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[attachmentsArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
[tableView reloadData];
}
}
update - placed break points at canEditRowAtIndexPath,editingStyleForRowAtIndexPath, commitEditingStyle but the control never goes to commitEditingStyle. It just keeps looping between the first two.

Xcode - TableView -> delete row - localNotification

My problem is that my delete method do not delete the right row!
In this tableView the user can save localNotifications for a special date. So if I create more than one localNot, the delete method does not delete the selected row! It deletes the last entry only ...
26.02.2012 delete third
27.02.2012 delete second
etc... delete first
Whats wrong?
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if(tableView.editing && editingStyle== UITableViewCellEditingStyleDelete)
{
[tableView beginUpdates];
[[UIApplication sharedApplication] cancelLocalNotification:notification];
[notificationsArray removeObjectAtIndex:indexPath.row];
NSLog(#"%#", notification);
// Animate the deletion from the table.
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
[tableView endUpdates];
[tableView reloadData];
}
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
// UITableViewCellEditingStyleNone;
return YES;
}

Resources