I am working on the app which contains one of the grouped section UITableView with edit/delete functionality. Tableview is looked like this
-(IBAction) EditTable:(id)sender
{
if (self.editing)
{
[super setEditing:NO animated:NO];
[libtable setEditing:NO animated:NO];
[libtable reloadData];
[tabledit setTitle:#"Edit" forState:UIControlStateNormal];
}
else
{
[super setEditing:YES animated:YES];
[libtable setEditing:YES animated:YES];
[libtable reloadData];
[tabledit setTitle:#"Done" forState:UIControlStateNormal];
}}
- (void)tableView:(UITableView *)aTableView commitEditingStyle:
(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[self delete];
}
}
On the very first row of each section I have disable the editing mode with this method:
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return indexPath.row > 0;}
It is working fine but you can see in this image :
The very first row of each section because of non-editable mode doesn't move to right side when I click on edit mode. I need these 0 index row also in right side on the click of edit button same like other rows. I didn't find any method to resolve this. If anyone has knowledge on this concept please help me out.
Thanks in advance.
Try like below
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 0) //for each section of first row return UITableViewCellEditingStyleNone
return UITableViewCellEditingStyleNone;
return UITableViewCellEditingStyleDelete;
}
i forgot see your -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
//you may comment this method
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES; //return for every cell as editable
}
if u run and in editing mode u will get like this
try to use below delegate method to decide editing style for cells
-(UITableViewCellEditingStyle)tableView:(UITableView*)tableView editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath {
if(indexPath.row ==0 ){
return UITableViewCellEditingStyleNone;
}else{
return UITableViewCellEditingStyleInsert;
}
}
and change your method canEditRowAtIndexPath to =>
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
Related
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.
//
}
}
I have implemented UITableview edit mode when a button is clicked but every time it goes to edit mode and I click on the deletion button, nothing happens. I have a view controller with a UITableview on it. I have set my delegate and tableview source as well as all of my editing callbacks. Everything is working (like reordering cells) but whenever I try to delete by pressing the delete control button, the delete button doesn't show up.
I am desperate since it seems like a really simple problem but no matter what I try it doesn't seem to work.
This is how I am implementing edit mode
- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:
(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
- (BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[favoriteCurrencyValueList removeObjectForKey:[favoriteCurrencyList objectAtIndex:indexPath.row]];
[favoriteCurrencyList removeObjectAtIndex:indexPath.row];
NSUserDefaults *defaultSettings = [NSUserDefaults standardUserDefaults];
[defaultSettings setObject:favoriteCurrencyList forKey:#"FavoriteCurrencies"];
[defaultSettings setObject:favoriteCurrencyValueList forKey:#"PastValues"];
[defaultSettings synchronize];
[self.favoriteCurrencyTable beginUpdates];
[self.favoriteCurrencyTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:(UITableViewRowAnimation)UITableViewRowAnimationLeft];
[self.favoriteCurrencyTable endUpdates];
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
indexPathSelected = indexPath;
//[self.view endEditing:YES];
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
[self.favoriteCurrencyList exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
[[NSUserDefaults standardUserDefaults] setObject:self.favoriteCurrencyList forKey:#"FavoriteCurrencies"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
This is what sets the edit mode
- (IBAction)editButtonPressed:(UIBarButtonItem *)sender {
if (self.editing && self.favoriteCurrencyTable.editing) {
self.editing = NO;
[self.favoriteCurrencyTable setEditing:NO animated:YES];
[self.editButton setTitle:#"Edit"];
}
else {
self.editing = YES;
[self.favoriteCurrencyTable setEditing:YES animated:YES];
[self.editButton setTitle:#"Done"];
}
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if (([touch.view isKindOfClass:[UIButton class]] && touch.view.tag==<Button_TAG>)) {
// prevent recognizing touches on the slider
return NO;
}
return YES;
}
For tap gestures add the delegate and write the above code.
You need to implement the following method:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
} 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
}
}
If delete does not work then it is a problem of your table view delegate or datasource methods called.Check these methods should definitely be called
1.tableView:editingStyleForRowAtIndexPath:
2.tableView:titleForDeleteConfirmationButtonForRowAtIndexPath:
3.tableView:shouldIndentWhileEditingRowAtIndexPath:
You try to check if titleforDeleteConfirmationbutton is called at every time you click edit and check how many times it calls and also check if you are giving indent return value as yes in shouldIndentwhileediting method.
check your gesture recognizers.
Here is the code:
-(void)buttonEditPressed:(id)sender{
if(_tableView.isEditing){
[_tableView setEditing:NO animated:YES];
[_buttonEdit setTitle:#"Edit" forState:UIControlStateNormal];
} else {
[_tableView setEditing:YES animated:YES];
[_buttonEdit setTitle:#"Done" forState:UIControlStateNormal];
}
[_tableView reloadData];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
DLog(#"");
return UITableViewCellEditingStyleDelete;
}
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//Delete the rows at the Indexpath.
//edit data model
[_tableView reloadData];
}
here is the result:
Swipe delete works. When the edit button is pressed however, the cell animates to the right displaying the space for the red delete button on the left, but that button never appears!
What should I do?
Try this library: https://github.com/CEWendel/SWTableViewCell
it's great documented, easy to use and great for what you want.
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.
Occasionally, when UITableView is in editing style and when you trigger the delete button, this button may not show. It seldom happen, but did anybody get the same issue too?
Post some code:
In viewDidLoad,
[[self tableView] setEditing:YES animated:YES];
table view delegate:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
//do sth.
}
}