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.
}
}
Related
In my TableView, I allow users to delete their posts using the swipe to delete:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//add code here for when you hit delete
}
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return YES if you want the specified item to be editable.
return YES;
}
However, I also allow users to tap the cell to open up a detail view of the post:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// segue and stuff
}
The problem is, when the user swipes to delete, it also triggers the didSelect method and opens up the detail view. How can I make sure didSelectRowAtIndexPath only triggers on taps, while the delete triggers on a drag swipe?
You should have add the following delegate method as well:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return YES - we will be able to delete all rows
return YES;
}
The whole implementation would be
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return YES - we will be able to delete all rows
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
}
}
Based on your updated question, then I would add a edit button on the right top side of the tableview to inform user to delete the selected cell. Check the following threads
change Table to Edit mode and delete Rows inside a normal ViewController
iOS delete a tableview row
Here is also good tutorial, it is worth to check
http://www.appcoda.com/model-view-controller-delete-table-row-from-uitableview/
If you're using a UITableViewController + UIStoryboard and connect your detail view segue to push in your cell prototype selection outlet, this works as expected (didSelectRowAtIndexPath is not called when swiping):
#interface TableViewController ()
#property (strong, nonatomic) NSMutableArray *cellTitles;
#end
#implementation TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.cellTitles = [#[#"Apple", #"Banana", #"Carrot", #"Millenium Falcon"] mutableCopy];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.cellTitles.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CellId" forIndexPath:indexPath];
cell.textLabel.text = self.cellTitles[indexPath.row];
return cell;
}
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.cellTitles removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
I wanna delete a particular row while I drag it.
Here is part of code.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableData removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tableData removeObjectAtIndex:indexPath.row];
[tableView reloadData];
}
}
Do you set delegate ?
self.tableView.delegate = self;
With corresponding How do I get the delete button to show when swiping on a UITableViewCell?
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return YES if you 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 from model
// [self.tableView reloadData]
}
}
You can put on breakpoints into your code for check call functions
yagnesh dobariya's answer is a working solution, and I would like to propose a modification that makes it more efficient.
Since you are only deleting one row at a time, you can keep using -deleteRowsAtIndexPaths:withRowAnimation: as in your original code. Just sandwich it between -beginUpdates and -endUpdates.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tableView beginUpdates];
[tableData removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
}
}
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 am using UITableViewController, I want to swipe delete cell. Already connected to dataSource and delegate.
it randomly detecting,
some times console prints
attempting to set a swipe to delete cell when we already have one....that doesn't seem good
He is my code,
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
}
}
Edit
My cellForRow AtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kFavouriteCell forIndexPath:indexPath];
[self customizeFavouriteCell:cell withIndexPath:indexPath];
return cell;
}
You need to delete the data for the cell, and delete the cell. Using deleteRowsAtIndexPaths:withRowAnimation:.
Inside this protocol function:
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// ... delete data here ...
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
Let me know if you have a more specific question about doing either of these.
You can use this:
- (void)tableView:(UITableView *)tableView commitEditingStyle:
(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//add code here for when you hit delete
}
}
I have a uitableviewdelegate class that is delegate of two uitableview. I want that one of them has remove button and the other do not have remove button. I used below code:
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
if(tableView==self.firstTableView){
return #"Remove";
}else
return #"";
}
button my code do not disable editing style for secondTableView it just set empty text as title. How can I do this?
EDIT
I also use below methods:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == slef.FirstTableView){
//Some codes
}
}
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == self.FirstTableView){
//Some codes
}
}
but any of them was not helpful.
Please implement this delegate method.
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView==self.firstTableView) {
return NO;
}
return YES;
}
Use delegate method
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
Here you can return UITableViewCellEditingStyleNone or UITableViewCellEditingStyleDelete for desired table