delete in tableView - iphone

I just add this methods in .h file :
- (IBAction)EditTable:(id)sender;
- (IBAction)DeleteButtonAction:(id)sender;
and in .m file :
(IBAction)DeleteButtonAction:(id)sender{
[tableList removeLastObject];
[Table reloadData];
}
(IBAction) EditTable:(id)sender{
if(self.editing)
{
[super setEditing:NO animated:NO];
[Table setEditing:NO animated:NO];
[Table reloadData];
[self.navigationItem.leftBarButtonItem setTitle:#"Edit"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStylePlain];
}
else
{
[super setEditing:YES animated:YES];
[Table setEditing:YES animated:YES];
[Table reloadData];
[self.navigationItem.leftBarButtonItem setTitle:#"Done"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStyleDone];
}
}
when I run the program and click the delete button (red button) the program is stop !
whats the problem ? please any help ?
you are evil :(
OK, again my code in .h file is :
- (IBAction)EditTable:(id)sender;
- (IBAction)DeleteButtonAction:(id)sender;
and in .m file is :
- (IBAction)DeleteButtonAction:(id)sender{
[tableList removeLastObject];
[Table reloadData];
}
- (IBAction) EditTable:(id)sender{
if(self.editing)
{
[super setEditing:NO animated:NO];
[Table setEditing:NO animated:NO];
[Table reloadData];
[self.navigationItem.leftBarButtonItem setTitle:#"Edit"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStylePlain];
}
else
{
[super setEditing:YES animated:YES];
[Table setEditing:YES animated:YES];
[Table reloadData];
[self.navigationItem.leftBarButtonItem setTitle:#"Done"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStyleDone];
} }
when I run the program and click the delete button (red button) the program is crash ! whats the problem ? please any help ?

If I am not wrong then you want to delete the cell of the tableView when delete button is clicked....
You need to call one other method of tableview:
//To Delete the Data
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
// Updates the appearance of the Edit|Done button as necessary.
[super setEditing:editing animated:animated];
[tblViewBM setEditing:editing animated:YES];
// Disable the add button while editing.
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:
(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//Use your Array from which you need to delete the data.
NSMutableDictionary *dict=(NSMutableDictionary *)[appDel.BookMarkAry objectAtIndex:indexPath.row];
type=[dict valueForKey:#"type"];
[appDel.BookMarkAry removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
I am sure this will Definately help you to delete the cell with data from array also.

Related

Push to new view from SearchDisplayController

Trying to push to a new view from the results of a search and for some reason the code isn't being called. Sorry I can't be more descriptive.
Here's the code:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.table deselectRowAtIndexPath:indexPath animated:YES];
int selectedTemplateID = [((CollectionItem*)[cllct objectAtIndex:indexPath.row]).tID intValue];
ACollection *newView = [[ACollection alloc] initWithID:selectedTemplateID];
newView.template = [cllct objectAtIndex:indexPath.row];
if(tableView == self.table) {
[self.navigationController pushViewController:newView animated:YES];
newView.theTitle = [self.table cellForRowAtIndexPath:indexPath].textLabel.text;
}
if(tableView == self.searchDisplayController.searchResultsTableView) {
NSLog(#"Should push");
[self.navigationController pushViewController:newView animated:YES];
newView.theTitle = [self.table cellForRowAtIndexPath:indexPath].textLabel.text;
}
}
Thanks
First check the datasource and delegat of tableview.
remove [self.table deselectRowAtIndexPath:indexPath animated:YES]; and check once.

UITableViewRowAnimationFade Not Working

So, this issue follows on from a previous issue, but I decided to post a new question to keep things relevant and tidy.
Basically, when the following piece of code is called, there is no difference between UITableViewRowAnimationFade and UITableViewRowAnimationNone:
- (void) setEditing:(BOOL)editing animated:(BOOL)animated
{
[tvController.tableView beginUpdates];
if (editing == YES) {
[tvController.tableView deleteRowsAtIndexPaths:[settingsArray objectAtIndex:0] withRowAnimation:UITableViewRowAnimationFade];
}else {
UITableViewRowAnimation animation = animated ? UITableViewRowAnimationFade : UITableViewRowAnimationNone;
[tvController.tableView reloadRowsAtIndexPaths:[settingsArray objectAtIndex:0] withRowAnimation:animation];
[tvController.tableView reloadSectionIndexTitles];
self.navigationItem.hidesBackButton = editing;
}
[tvController.tableView endUpdates];
}
Greatly appreciate any help. It still enters editing mode, but does not animate into it, depsite YES being passed into animated.
EDIT: The animation works fine when I'm actually deleting things using the following code:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *stuff = [documentsPath stringByAppendingPathComponent:#"stuff.plist"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:stuff];
if (fileExists) {
NSMutableDictionary *propertyList = [[NSMutableDictionary alloc] initWithContentsOfFile:enteredPlaces];
[propertyList removeObjectForKey:[[settingsArray objectAtIndex:1] objectAtIndex:indexPath.row]];
[propertyList writeToFile:stuff atomically:YES];
}
[[settingsArray objectAtIndex:1] removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
}
It just doesn't work when the user presses the edit button and the table goes into editing mode, the tableview just snaps statically into edit mode.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[db DeleteData:[NSString stringWithFormat:#"%d",indexPath.row]];
[data removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation: UITableViewRowAnimationFade];
}
}
I have same scenario in my application, I was facing the similar issue. I guess below function will solve your issue :
- (void) setEditing:(BOOL)editing animated:(BOOL)animated
{
[tvController.tableView setEditing:editing animated:animated]; //this LOC is added
[tvController.tableView beginUpdates];
if (editing == YES) {
[tvController.tableView deleteRowsAtIndexPaths:[settingsArray objectAtIndex:0] withRowAnimation:UITableViewRowAnimationFade];
} else {
UITableViewRowAnimation animation = animated ? UITableViewRowAnimationFade : UITableViewRowAnimationNone;
[tvController.tableView reloadRowsAtIndexPaths:[settingsArray objectAtIndex:0] withRowAnimation:animation];
[tvController.tableView reloadSectionIndexTitles];
self.navigationItem.hidesBackButton = editing;
}
[tvController.tableView endUpdates];
}
-(void)deleteRecord:(NSInteger)recordNo:(NSInteger)sectionNo:(BOOL)isEditMode:(BOOL)isAnimate {
if(isEditMode){
NSIndexPath *indexP=[NSIndexPath indexPathForRow:recordNo inSection:sectionNo];
[tvController.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexP, nil] withRowAnimation:UITableViewRowAnimationLeft];
}
else {
if(isAnimate)
[tvController.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexP, nil] withRowAnimation:UITableViewRowAnimationFade];
else
[tvController.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexP, nil] withRowAnimation:UITableViewRowAnimationNone];
[tvController.tableView reloadSectionIndexTitles];
self.navigationItem.hidesBackButton = editing;
}
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:#selector(reload) userInfo:nil repeats:NO];
}
-(void)reload {
[table reloadData];
}
if the only thing that you are annoyed by is the fact that the delete doesn't fade, it is because you are anticipating something that the interface doesn't provide.
you mentioned that you know individual cells can be set to be non-editable, and this is the standard way to do this. the mechanism simply does not anticipate not using the UITableViewDataSource to provide the information about what to display (or, in your case, what is in the data being changed) simply by hitting the edit/done button.
by trying to combine the two, you are confusing the animation that is supposed to happen.
probably the best you can do is something like the following (and the animation duration and length could possibly be 0, since you're asking for separate animation in the tableView), which will cause your animation to occur after the animation that opens up editing mode.
- (void) setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated]; // possibly not necessary depending upon your class hierarchy
[tvController.tableView setEditing:editing animated:animated];
[UIView animateWithDuration:0.25 delay:0.05 options:UIViewAnimationCurveLinear
animations:^{
[tvController.tableView beginUpdates];
if (editing == YES) {
[tvController.tableView deleteRowsAtIndexPaths:[settingsArray objectAtIndex:0] withRowAnimation:UITableViewRowAnimationFade];
} else {
UITableViewRowAnimation animation = animated ? UITableViewRowAnimationFade : UITableViewRowAnimationNone;
[tvController.tableView reloadRowsAtIndexPaths:[settingsArray objectAtIndex:0] withRowAnimation:animation];
[tvController.tableView reloadSectionIndexTitles];
self.navigationItem.hidesBackButton = editing;
}
[tvController.tableView endUpdates];
}
completion:nil];
}

iPhone:Add & Delete Row in UITableview with editing mode

What I want to do is:
Setting editing mode on NavigationBar in UITableView adds an edit button on the left side of the UINavigationBar. When I click this button, I'd like an add button to appear on the right side of the NavigationBar.
When I click on the add button, add a row to the NSMutableArray and update the table.
So please give me ideas, code, or links to develop this functionality.
- (IBAction)DeleteButtonAction:(id)sender
{
[arry removeLastObject];
[Table reloadData];
}
- (IBAction) EditTable:(id)sender
{
if(self.editing)
{
[super setEditing:NO animated:NO];
[Table setEditing:NO animated:NO];
[Table reloadData];
[self.navigationItem.leftBarButtonItem setTitle:#"Edit"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStylePlain];
}
else
{
[super setEditing:YES animated:YES];
[Table setEditing:YES animated:YES];
[Table reloadData];
[self.navigationItem.leftBarButtonItem setTitle:#"Done"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStyleDone];
}
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.editing == NO || !indexPath) return UITableViewCellEditingStyleNone;
if (self.editing && indexPath.row == ([arry count]))
{
return UITableViewCellEditingStyleInsert;
} else
{
return UITableViewCellEditingStyleDelete;
}
return UITableViewCellEditingStyleNone;
}
- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[arry removeObjectAtIndex:indexPath.row];
[Table reloadData];
} else if (editingStyle == UITableViewCellEditingStyleInsert)
{
[arry insertObject:#"Tutorial" atIndex:[arry count]];
[Table reloadData];
}
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
toIndexPath:(NSIndexPath *)toIndexPath
{
NSString *item = [[arry objectAtIndex:fromIndexPath.row] retain];
[arry removeObject:item];
[arry insertObject:item atIndex:toIndexPath.row];
[item release];
}
}
Create a private property to have a reference to the Edit button:
#property (nonatomic) UIBarButtonItem *editButton;
In viewDidLoad:, init the button and assign it to the right button of the navigationItem:
self.editButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"Edit", nil) style:UIBarButtonItemStylePlain target:self action:#selector(toggleEdit:)];
self.navigationItem.rightBarButtonItem = self.editButton;
Create a method to invoke when the Edit button is tapped. Will change the table's editing mode and update the Edit button title:
- (IBAction)toggleEdit:(id)sender {
[self.myTableView setEditing:!self.myTableView.editing animated: YES];
[self.editButton setTitle:self.myTableView.editing ? NSLocalizedString(#"Done", nil) : NSLocalizedString(#"Edit", nil)];
}

UITableView Editing Not working

I have a UITableView which lies in a NavigationController that is in a popover. So, I put an edit/done UIToolBarItem which works fine and you can see the delete accessory icon showing up. However, when I rearrange the items, the edit/done button stops working...
I debugged and it still gets called but it doesn't seem to update and is sort of preventing any further updates from other buttons. Is this possibly a problem with the simulator?
Thanks for the help in advance!
Edit: Adding code
addButton = [[UIBarButtonItem alloc] initWithTitle:#"Edit" style:UIBarButtonItemStyleBordered target:self action:#selector(editTable:)];
And later on the method:
- (IBAction) editTable: (id) sender
{
if ([[addButton title] isEqualToString:EDIT_STRING]) {
[addButton setTitle:DONE_STRING]; //enabling edit mode
[super setEditing:YES animated:YES];
[[self tableView] setEditing:YES animated:YES];
}
else {
[addButton setTitle:EDIT_STRING]; //done with editing
[[self tableView] setEditing:NO animated:YES];
[super setEditing:NO animated:YES];
[[self tableView] setNeedsDisplay];
[[self tableView] reloadData];
}
}
The second part is definitely called as the addButton text is changing. However, it stops working once I make an edit (such as re-arranging a row)
Alright,
I seemed to have called [tableView beginUpdates] and never called endUpdates, which was causing all updates to not be registered. If anyone else does that...
- (void)tableView:(UITableView *)tableview commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tv reloadData];
}
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[self.tv setEditing:editing animated:YES];
}

How do I popViewControllerAnimated after a delay?

I have a UITableViewController that is presented with a list of choices. After the user taps one, I'd like to return to the previous view. The return seems too quick with the code I'm using though. I'd like to pause for 0.2 seconds or so to give the user time to see their selection become checked. Here's the code I'm using now:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger oldSelection = [[selectedCriteria objectAtIndex:criteriaSection] integerValue];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// Since there's a "none" selection, we don't deselect if the user taps the one that's already selected
if ([indexPath row] != oldSelection + 1) {
NSIndexPath *selectionIndexPath = [NSIndexPath indexPathForRow:oldSelection+1 // Shift down for "None"
inSection:[indexPath section]];
UITableViewCell *checkedCell = [tableView cellForRowAtIndexPath:selectionIndexPath];
[checkedCell setAccessoryType:UITableViewCellAccessoryNone];
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
[selectedCriteria replaceObjectAtIndex:criteriaSection
withObject:[NSNumber numberWithUnsignedInteger:[indexPath row]-1]];
}
[[self navigationController] popViewControllerAnimated:YES];
}
Is there a good way to add a short delay before the view controller is popped?
Hope this help.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger oldSelection = [[selectedCriteria objectAtIndex:criteriaSection] integerValue];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// Since there's a "none" selection, we don't deselect if the user taps the one that's already selected
if ([indexPath row] != oldSelection + 1) {
NSIndexPath *selectionIndexPath = [NSIndexPath indexPathForRow:oldSelection+1 // Shift down for "None"
inSection:[indexPath section]];
UITableViewCell *checkedCell = [tableView cellForRowAtIndexPath:selectionIndexPath];
[checkedCell setAccessoryType:UITableViewCellAccessoryNone];
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
[selectedCriteria replaceObjectAtIndex:criteriaSection
withObject:[NSNumber numberWithUnsignedInteger:[indexPath row]-1]];
}
[self performSelector:#selector(dismissController) withObject:nil afterDelay:0.2];
}
This give you a 0.2 seconds delay to call the function "dismissController".
The function "dismissController".
- (void) dismissController {
[[self navigationController] popViewControllerAnimated:YES];
}
Have you tried -performSelector:withObject:afterDelay ?
sleep(0.2) worked for me