I need to add the "swipe to delete" feature on my UITableView, I tried with the following code:
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[indexProdotti removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else {
NSLog(#"Unhandled editing style! %ld", (long)editingStyle);
}
}
The problem is that there is only a limited space I have to swipe in order to show the delete button, the rest of the cell if swiped gives no result.. Am I missing something in my implementation?
Related
I Have a tableview and a cell in it :D with disclosure accessory selected. I created all ui elements with storyboard.
(didn't put a view in content of the cell)
I'm using SWTableViewCell to implement just swipe-to-delete, but everything seems work fine except when i put a breakpoint on to a method
#pragma mark -SWTableViewDelegate-
-(void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityButtonWithIndex:(NSInteger)index
{
NSIndexPath *path=[self.table indexPathForCell:cell];
[anArray removeObjectAtIndex:path.row];
[self.table deleteRowsAtIndexPaths:#[path] withRowAnimation:UITableViewRowAnimationRight];
}
and this will help you to understand what i simply done here
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
SWTableViewCell *cell=(SWTableViewCell*)[tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
if (!cell)
{
NSMutableArray *rightUtilityButtons = [NSMutableArray new];
[rightUtilityButtons sw_addUtilityButtonWithColor:[UIColor colorWithRed:1.0f
green:0.231f
blue:0.188
alpha:1.0f]
title:#"Delete"];
cell = [[SWTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell" containingTableView:_table leftUtilityButtons:nil rightUtilityButtons:rightUtilityButtons];
}
NSMutableDictionary *deDict=[[NSMutableDictionary alloc]initWithDictionary:[anArray objectAtIndex:indexPath.row]];
cell.textLabel.text= [deDict objectForKey:#"Name"];
return cell;
}
Yet, I don't get any error, because when i try to swipe on simulator, it simply does not work..
No need to use SWTableViewCell just Override following built-in methods of UITableView delegate for support conditional editing & swipe to delete
for conditional editing -- Optional --
//Override this method only if you are going to be returning NO for some items.
//By default, all items are editable.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return YES if you want the specified item to be editable.
return YES;
}
for swipe to delete
// Override to support editing the table view.
// for swipe to delete
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//add code here for when you hit delete
}
}
I want to stop all the rows being deleted from my table view by user so at least one row is always present.
Im still geting to grips with programming and objective c.
Heres what I tried:
if (indexPath.section != 0 | editingStyle == UITableViewCellEditingStyleDelete) {
[self.managedObjectContext deleteObject:[self.circuits objectAtIndex:indexPath.row]];
[self.appDelegate saveContext];
[self.circuits removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
then
if (indexPath.section > 1 | editingStyle == UITableViewCellEditingStyleDelete) {
[self.managedObjectContext deleteObject:[self.circuits objectAtIndex:indexPath.row]];
[self.appDelegate saveContext];
[self.circuits removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
That had no effect in that I can still delete the first row. So then I tried
- (BOOL)tableView:(UITableView *)commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0)
return NO;
return YES;
}
Still cant prevent the first row from being deleted
You need to implement the UITableViewDataSource method tableView:canEditRowAtIndexPath: and return NO for the indexPath.row 0.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return indexPath.row > 0;
}
Try moving your code into
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
Try this code :-
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row==0)
return NO;
else
return YES;
}
It will surely help you
Want to delete a row when you swipe with your finger on the cell.
I got this:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Delete");
}
After running the Delete button appears but when I press on it nothing happens. What do I need to insert in to the brackets?
And my .m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *str = cell.textLabel.text;
NSLog(#"U selected %#", str);
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.dataSourceArray removeObjectAtIndex:indexPath.row];//or something similar to this based on your data source array structure
//remove the corresponding object from your data source array before this or else you will get a crash
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.array = #[#"test",#"test"];
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:nil action:#selector(updateArray) forControlEvents:UIControlEventValueChanged];
self.refreshControl = refreshControl;
}
Try this,
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.array removeObjectAtIndex:indexPath.row];//or something similar to this based on your data source array structure
//remove the corresponding object from your data source array before this or else you will get a crash
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
Say I have a small tableview(no scroll) and when a user taps an option, it changes some setting in the app. I used this function:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//some code here
}
Now I want to use UITableViewCellAccessoryCheckmark for the option that is selected, I mean it should display a checkmark in the cell the user selected (and remove checkmark from previously selected option). How do I do that?
How about
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
for (UITableViewCell *cell in [tableView visibleCells]) {
cell.accessoryType = UITableViewCellAccessoryNone;
}
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
Just use the accessoryTypeproperty of a UITableViewCell.
#property(nonatomic) UITableViewCellAccessoryType accessoryType
Like this:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//code for reusing or init a new cell goes here
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
I am having a big issue trying to delete from an UITableView.
Just for note, I have the same code running fine on another view and thats why it's making me crazy.
The only difference is that on the other view, my array is in a property. But I tried changing my tmpArray to a property and nothing changed. Here's the code and the error after:
-(void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tmpArray removeObjectAtIndex:indexPath.row];
[tbvPlaylist deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
Error:
*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1912.3/UITableView.m:1046
Let me now if you need more info.
Is tmpArray the same ivar that is providing the count of rows e.g.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
return tmpArray.count;
}
Also at the moment you call
[tbvPlaylist deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
tableView:numberOfRowsInSection: should return the
numberOfRowsBeforeDelete - numberOfRowsBeingDeleted