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.
Related
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.
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;
}
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.
I'm fairly new to this and am stuck on a problem. I can't get the ability to edit a table view i've created...
I have a button on the top right corner of the app that says "Edit" and - (IBAction)editTable:(id)sender, I've tried numerous attempts to get to edit this list I've created...
#implementation XYZViewController
#synthesize animalNames;
- (void)viewDidLoad
{
[super viewDidLoad];
//create array called animal names
animalNames = [[NSArray alloc]initWithObjects:
#"Cow",
#"Dog",
#"Cat",
#"Dear",
#"Penguin",
#"Lion",
#"Leapord",
#"Eal",
#"Snake",
#"Moth",
#"Cheetah",
#"Turtle"
, nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.animalNames count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier = #"MainCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.textLabel.text = [self.animalNames objectAtIndex:indexPath.row];
return cell;
}
- (IBAction)editTable:(id)sender
{
NSLog(#"Editing");
[super setEditing:TRUE];
[self.tableView setEditing:TRUE];
self.editing = YES;
}
#end
Using Xcode 5.
You need to implement following methods to support table view editing:
- (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
{
return UITableViewCellEditingStyleDelete;
}
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO;
}
You should declare animalNames as NSMutableArray as you are going edit it. Then you need to override some delegate methods to perform edit in tableview.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
NSString *sourceItem = _animalNames[sourceIndexPath.row];
[_animalNames removeObjectAtIndex:sourceIndexPath.row];
[_animalNames insertObject:sourceItem atIndex:destinationIndexPath.row];
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete){
[_animalNames removeObjectAtIndex:indexPath.row];
[_tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
You can stop editing in editTable method like
- (IBAction)editTable:(id)sender
UIBarButtonItem *button = (UIBarButtonItem *)sender;
if ([button.title isEqualToString:#"Edit"]) {
button.title = #"Done";
[self.tableView setEditing:TRUE];
} else {
button.title = #"Edit";
[self.tableView setEditing:NO];
}
}
Hope this will help you
To edit the table view you need to call
[self.tableView setEditing:YES animated:YES]; on the table which you are using then
You have to implement the delegate method
tableView:commitEditingStyle:forRowAtIndexPath:
method to enable the deleting of rows. In order to delete the rows you need to use
Then Do this..
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//Remove the object at the index we need to delete
[YourTableArray removeObjectAtIndex:indexPath.row];
//Delete the rows at the Indexpath.
[YourTable deleteRowsAtIndexPaths:[[NSArray alloc]initWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationRight];
[reminder_table reloadData];
//Set the table to editing NO.
[YourTable setEditing:NO animated:YES];
}
Please find below link for your reference
https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/ManageInsertDeleteRow/ManageInsertDeleteRow.html#//apple_ref/doc/uid/TP40007451-CH10-SW19
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.
}
}