I'd like my cells to have not only the Delete button while editing but also others. So what is an actual way to do this? I've tried making own UIView object for editingAccessoryView property but the problem is being that the view appears only on direct edit (i.e. clicking Edit button in Navigation Bar) and completely ignores swipes across a cell. Also tableView:editActionsForRowAtIndexPath: method was given a try but didn't work, at least I didn't manage to figure out how to get what I want with its function. Thank you in advance!
You Try This Code
-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction *button = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:#"Button 1" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
{
NSLog(#"Action to perform with Button 1");
}];
button.backgroundColor = [UIColor greenColor]; //arbitrary color
UITableViewRowAction *button2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:#"Button 2" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
{
NSLog(#"Action to perform with Button2!");
}];
button2.backgroundColor = [UIColor blueColor]; //arbitrary color
return #[button, button2]; //array with all the buttons you want. 1,2,3, etc...
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
// you need to implement this method too or nothing will work:
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES; //tableview must be editable or nothing will work...
}
You CanTry also this code
.H File
{
NSMutableArray *dataArray;
}
#property (weak, nonatomic) IBOutlet UITableView *atableView;
#property(nonatomic,strong) NSMutableArray *dataArray;
**`.M file`**
- (void)viewDidLoad
{
[super viewDidLoad];
self.dataArray = [[NSMutableArray alloc] initWithObjects:#"Saurabh Sharma",#"Deepesh Jain",#"Ashish Sharma",#"Chandan",#"kanhaiya",#"Suchitra Bohra",#"Neha",#"Ghanshyam",nil];
self.title = #"Move Rows";
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:#"Edit" style: UIBarButtonItemStyleBordered target:self action:#selector(editButton:)];
[self.navigationItem setRightBarButtonItem:editButton];
// Do any additional setup after loading the view from its nib.
}
-(void)editButton:(UIBarButtonItem *)button
{
{
if(self.editing)
{
[super setEditing:NO animated:NO];
[atableView setEditing:NO animated:NO];
[atableView reloadData];
[self.navigationItem.rightBarButtonItem setTitle:#"Edit"];
[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStylePlain];
}
else
{
[super setEditing:YES animated:YES];
[atableView setEditing:YES animated:YES];
[atableView reloadData];
[self.navigationItem.rightBarButtonItem setTitle:#"Done"];
[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone];
}
}
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [dataArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifair=#"cellIdentifair";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifair];
if(cell==nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifair];
}
cell.textLabel.text = [self.dataArray objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
#pragma delete row in table view
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionView *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 10; // This is the minimum inter item spacing, can be more
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return TRUE;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView beginUpdates];
[dataArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
[tableView endUpdates];
NSLog(#"Methods called when row is deleted");
}
#pragma Move Table View Delegte
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
NSString *item = [self.dataArray objectAtIndex:fromIndexPath.row];
[self.dataArray removeObject:item];
[self.dataArray insertObject:item atIndex:toIndexPath.row];
}
Works in Swift as well.
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
var completionButton = UITableViewRowAction(style: .Normal,
title: "Complete",
handler: {(action, index) in
/* Completion Code */
println("Complete")})
completionButton.backgroundColor = .lightGrayColor()
return [completionButton]
}
Also for iOS 11 you should use
- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView leadingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath - for right swipe
and
- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath - for left swipe
For ex.:
- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
UIContextualAction* action = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:#"Title" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
// Do something
completionHandler(YES);
}
UISwipeActionsConfiguration * actionsConfiguration = [UISwipeActionsConfiguration configurationWithActions:#[action]];
return actionsConfiguration;
}
Related
I have created a stepper in a CustomCell of a UITableView. However, I would like the stepper to only be visible when a particular row is selected. To this end, I tried the following:
In tableView:cellForRowAtIndexPath:
cell.customStepper.hidden=NO;
and in tableView:didSelectRowAtIndexPath:
cell.customStepper.hidden=YES;
But the stepper is still hidden. What am I missing?
#interface BRNCategoryViewController ()
{
NSMutableArray *arySelectCategory;
NSMutableArray *aryCategory;
}
- (void) viewDidLoad
{
arySelectCategory=[NSMutableArray new];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return aryCategory.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
BRNCategoryCell *cell=[[BRNCategoryCell alloc]initWithOwner:self];
if ([arySelectCategory containsObject:[aryCategory objectAtIndex:indexPath.row]])
{
cell.customStepper.hidden = NO;
}
else
{
cell.customStepper.hidden = YES;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if ([arySelectCategory containsObject:[aryCategory objectAtIndex:indexPath.row]])
{
[arySelectCategory removeObject:[aryCategory objectAtIndex:indexPath.row]];
}
else
{
[arySelectCategory addObject:[aryCategory objectAtIndex:indexPath.row]];
}
[tblEventCategory reloadData];
}
In cellForRowAtIndexPath
cell.customStepper.hidden=YES;
And
In didSelectRowAtIndexPath
VideosCell *cell = (VideosCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.customStepper.hidden = NO;
I've been trying to stop the (-) delete indicator from showing up when I put a UITableView into editing mode.
Updates for clarity:
This is what the table looks like:
This is what I have setup in - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
When I tap reorder, I go into edit mode. I don't want these to show:
I've worked around the the 3rd screenshot by limiting the buttons shown from editActionsForRowAtIndexPath to just delete, but I don't even want to get to this part. I want Edit mode to be reorder only
/Updates
I've tried setting UITableViewCell.shouldIndentWhileEditing = NO
On the Cell directly:
cell.indentationLevel = -3;
cell.shouldIndentWhileEditing = NO;
In interface builder
In the appropriate delegate method
- (BOOL)tableView:(UITableView *)tableview shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"shouldIndentWhileEditingRowAtIndexPath");
return NO;
}
I'm also showing over items in the actions list. I hadn't planned on having delete at all when in Editing mode, just reodering. I've adjusted a few things so I only show the delete when a user swipes, but I'd rather the (-) not show at all:
- (void) endEditing
{
self.editMode = NO;
self.editControlButton.title = #"Reorder";
[self setEditing:NO animated:YES];
//[self.tableView reloadData];
}
- (void) startEditing
{
self.editMode = YES;
self.editControlButton.title = #"Done";
[self.tableView setEditing:YES animated:YES];
}
#pragma - mark UITableViewDelegate
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
- (BOOL)tableView:(UITableView *)tableview shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"shouldIndentWhileEditingRowAtIndexPath");
return NO;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
TCORead *item = [self.fetchedResultsControllerDataSource selectedItem];
manager.currentRead = item;
}
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:#"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
[self.tableView setEditing:NO];
}];
//workaround, rdar://17969970
//normally don't want to be able to get into this menu when reordering
if (!self.editMode) {
UITableViewRowAction *shareAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:#"Share" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
[self.tableView setEditing:NO];
}];
shareAction.backgroundColor = [UIColor grayColor];
UITableViewRowAction *doneAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:#"Done" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
[self.tableView setEditing:NO];
}];
doneAction.backgroundColor = [UIColor greenColor];
[self startEditing];
return #[deleteAction, doneAction, shareAction];
}
return #[deleteAction];
}
- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
[self endEditing];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
//empty on purpose
}
I was forcing the delete indicator to appear. Vanyas has a sample project that showed me what I was doing wrong.
My editingStyleForRowAtIndexPath method needed to look like this:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath: (NSIndexPath *)indexPath
{
//return UITableViewCellEditingStyleDelete;
return tableView.isEditing ? UITableViewCellEditingStyleNone: UITableViewCellEditingStyleDelete;
}
If you don't want to allow deleting, just reordering, then there is no need to do anything about indenting. Instead, just do:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleNone;
}
And don't implement the tableView:commitEditingStyle:forRowAtIndexPath: method at all.
You also avoid returning deleteAction from tableView:editActionsForRowAtIndexPath:.
That will prevent row deletion. You can now support reordering as needed.
I use custom cell.I want to move this cell (change position of the cells)
i used this delegations
UITableViewDataSource,UITableViewDelegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *Cellidentifier = #"Celledit";
CellEdit *editCell =[tableView dequeueReusableCellWithIdentifier:Cellidentifier];
if (!editCell) {
editCell = [[CellEdit alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Cellidentifier];
}
MItem *mItem = [mItems objectAtIndex:[indexPath row]];
---------------------------------
// set data 3 labales in the cell
---------------------------------
editCell.editdelegate = self;
return editCell;
editcell is my custom cell
[_tableViewEdit setEditing:YES animated:YES]; this line put in - (void)viewDidLoad
i want to edit it always
// table view change cells
- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleNone;
}
- (BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
NSInteger sourceRow = sourceIndexPath.row;
NSInteger destRow = destinationIndexPath.row;
id object = [minutesItems objectAtIndex:sourceRow];
[minutesItems removeObjectAtIndex:sourceRow];
[minutesItems insertObject:object atIndex:destRow];
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
for (UIControl *control in cell.subviews)
{
if ([control isMemberOfClass:NSClassFromString(#"UITableViewCellReorderControl")] && [control.subviews count] > 0)
{
for (UIControl *someObj in control.subviews)
{
if ([someObj isMemberOfClass:[UIImageView class]])
{
UIImage *img = [UIImage imageNamed:#"logocoffee.png"];
((UIImageView*)someObj).frame = CGRectMake(0.0, 0.0, 43.0, 43.0);
((UIImageView*)someObj).image = img;
}
}
}
}
}
}
to move the cell simulator not showing right side control icon
why this?
that means cant move costume cells?
Try this code,
You should use below two method,
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
Use this method for moving the row
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
[label1array exchangeObjectAtIndex:fromIndexPath.row withObjectAtIndex:toIndexPath.row];
[label2array exchangeObjectAtIndex:fromIndexPath.row withObjectAtIndex:toIndexPath.row];
[label3array exchangeObjectAtIndex:fromIndexPath.row withObjectAtIndex:toIndexPath.row];
}
Custom cells can be moved as well.
From your code samples we don't see where you are setting your tableview in the editing mode.
Perhaps the issue is because you are never setting the editing mode.
If so, try to add an Edit button in the title bar for example, which should be linked to a method such as:
- (IBAction) EditTable:(id)sender{
if(self.editing)
{
[super setEditing:NO animated:NO];
[yourTableView setEditing:NO animated:NO];
[yourTableView reloadData];
[self.navigationItem.leftBarButtonItem setTitle:#"Edit"];
}
else
{
[super setEditing:YES animated:YES];
[yourTableView setEditing:YES animated:YES];
[yourTableView reloadData];
[self.navigationItem.leftBarButtonItem setTitle:#"Done"];
}
}
I think this one should resolve your issue.
Edit: To resume in your case, as a minimum, you only need to set your table view in the editing mode in viewDidLoad, and to update it if needed:
[yourTableView setEditing:YES animated:YES];
[yourTableView reloadData];
and to implement both callbacks:
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
//Even if the method is empty you should be seeing both rearrangement icon and animation.
}
I tried with custom cells and it's working fine.
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
I am facing an issue to delete a row from a table view
I am using a custom button in a UITableviewCell
When I tap that button the application crashes
My Code is Below
- (void)viewDidLoad
{
[DatabaseFiles check_Create_DB];
FavviewArray = [DatabaseFiles getData];
FavviewArray = [FavviewArray valueForKey:#"Fav_Msg"];
[mytableview setDataSource:self];
[mytableview setDelegate:self];
}
-(void)viewWillAppear:(BOOL)animated
{
//============Custome Favourite Button================
UIImage *image = [UIImage imageNamed:#"FAv_On-iphone.png"];
UnFavbtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 28, 28)];
[UIButton buttonWithType:UIButtonTypeCustom];
[UnFavbtn setBackgroundImage:image forState:UIControlStateNormal];
UnFavbtn.backgroundColor = [UIColor clearColor];
//cell.accessoryView = button;
[UnFavbtn addTarget:self action:#selector(UnFavouriteClick) forControlEvents:UIControlEventTouchUpInside];
//==================================================
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [FavviewArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
// }
cell.textLabel.text = [FavviewArray objectAtIndex:indexPath.row];
cell.accessoryView =UnFavbtn;
//[self.mytableview reloadData];
return cell;
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectedrow = (indexPath.row);
NSLog(#"Selected Row====%d",lastindexpath.row);
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.editing)
{
return UITableViewCellEditingStyleDelete;
}
return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
lastindexpath = indexPath;
[self UnFavouriteClick];
// [FavviewArray removeObjectAtIndex:lastindexpath.row];
// [mytableview deleteRowsAtIndexPaths:[NSArray arrayWithObject:lastindexpath] withRowAnimation:UITableViewRowAnimationFade];
//[DatabaseOperation DeleteData:[Arrayid objectAtIndex:lastIndexPath.row+1 ]];
//[mytableview reloadData];
}
}
- (void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Button-iphone.png"]];
}
- (void)UnFavouriteClick
{
NSLog(#"Last Indexpath====%d",lastindexpath.row+1);
//[FavviewArray removeObject:selectedrow];
//[FavviewArray removeObjectAtIndex:lastindexpath.row];
[mytableview deleteRowsAtIndexPaths:[NSArray arrayWithObject:lastindexpath] withRowAnimation:UITableViewRowAnimationFade];
[DatabaseFiles DeleteData:[FavviewArray objectAtIndex:lastindexpath.row+1 ]];
[mytableview reloadData];
}
You should delete the object in your model first then call deleteRowsAtIndexPaths:withRowAnimation:
And you don't need to call -reloadData
-(void)unFavouriteRowAtIndexPath:(NSIndexPath *)indexPath
{
[DatabaseFiles DeleteData:[FavviewArray objectAtIndex:indexPath.row+1 ]];
[mytableview deleteRowsAtIndexPaths:#[indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}