Different ViewController called when TableView is Editing - objective-c

I have a simple grouped table view that when a cell is selected it pushes a View controller. The navigation bar's left item puts the table view in "editing mode." In editing mode you can move and delete each cell as wanted. What I want is to be able to push a separate View controller when it is editing mode then when it is in normal mode.
This is where the View Controller is being pushed:
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
...stuff that doesnt matter...
[[self navigationController] pushViewController:detailViewController animated:YES];
}
The BarButtonItems are being declared here:
[[self navigationItem] setLeftBarButtonItem:[self editButtonItem]];
This is where all the editing stuff is declared:
- (void)tableView:(UITableView *)atableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[atableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation: UITableViewRowAnimationFade];
}
}
- (void)tableView:(UITableView *)atableView
moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath
toIndexPath:(NSIndexPath *)destinationIndexPath
{
[[BNRItemStore sharedStore] moveItemAtIndex:[sourceIndexPath row]
toIndex:[destinationIndexPath row]];
}

When you push your view controller in the didSelectRowAtIndexPath, you can check if the tableview is in editing mode. If it is, you can push a different view controller.
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
...stuff that doesnt matter...
if(aTableView.editing) // The tableview is in editing mode
[[self navigationController] pushViewController:otherViewController animated:YES];
else [[self navigationController] pushViewController:detailViewController animated:YES];
}
editing is a property that returns YES if the tableview is currently in editing mode.

Related

Tableview won't reload data after selecting row

When I select any row in tableView data should be removed. I have set selected text in textField which is working, but tableView data is not cleared after selection, code given below.
Please help me.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.tblViewSearch deselectRowAtIndexPath:indexPath animated:YES];
TimeZoneCity *objCity = (TimeZoneCity *)[searchResult objectAtIndex:indexPath.row];
txtfieldTimeZone.text=objCity.timezone;
[searchResult removeAllObjects];
[self.tblViewSearch reloadData];
}
If you really want to remove the table view you could try this. This is a bad idea if you have set up your table view with interface builder, since it does the creation for you in this case.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.tblViewSearch deselectRowAtIndexPath:indexPath animated:YES];
TimeZoneCity *objCity = (TimeZoneCity *)[searchResult objectAtIndex:indexPath.row];
txtfieldTimeZone.text=objCity.timezone;
[self.tblViewSearch removeFromSuperview];
}
A better strategy might be to just hide it, and then show it again when needed. To make it look nicer you could add an animation too.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.tblViewSearch deselectRowAtIndexPath:indexPath animated:YES];
TimeZoneCity *objCity = (TimeZoneCity *)[searchResult objectAtIndex:indexPath.row];
txtfieldTimeZone.text=objCity.timezone;
[UIView animateWithDuration:0.3 animations:^{
self.tblViewSearch.alpha = 0.0;
}];
}

UITableViewCell on left swipe doesn't trigger editActionsForRowAtIndexPath

I have a scrollview which contains different subviews, one of which is tableview. I also have a UITapGestureRecognizer and UISwipeGestureRecognizer initialized on the scroll view. The tableview uses a nib to initialize its cell.
What I would like to do is that, when I swipe the cell to the left, I want a button to swipe in. I am able to trigger my handler when left swipe the cell. The problem is that it doesn't trigger the editActionsForRowAtIndexPath where I am handling the code to create a button.
Instead, it goes to editingStyleForRowAtIndexPath and ends executing showing a red icon with a minus sign to the left of the cell.
My swipe handler and related code is as below:
- (void)swipeLeftDirection:(UIGestureRecognizer*)gesture
{
if (!tblInviteesTable.editing)
{
CGPoint location = [gesture locationInView: tblInviteesTable];
iPath = [tblInviteesTable indexPathForRowAtPoint:location];
// quitEditing = NO;
OpenHouseTableViewCell *currentCell = (OpenHouseTableViewCell *)[self tableView:tblInviteesTable cellForRowAtIndexPath:iPath];
[currentCell setEditing:YES];
[tblInviteesTable setEditing:YES animated:YES];
[self tableView:tblInviteesTable editActionsForRowAtIndexPath:iPath];
}}
- (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) {
NSLog(#"Handled editing style! %ld", (long)editingStyle);
} else {
NSLog(#"Unhandled editing style! %ld", (long)editingStyle);
}
}
-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([tableView isEqual: tblInviteesTable]){
UITableViewRowAction *button = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:#"Resend" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(#"Resending email");
// [_delegate listingAgentsView: self selected: indexPath];
}];
button.backgroundColor = [UIColor redColor];
return #[button];
}
return #[];
}
Any help is greatly appreciated.

Swipe to delete

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
}
}

Delete from UITableView using an NSFetchedResultsController

I have a UITableView which is populated through a NSFetchedResultsController. When the user slides an item to the right I have a delete button appearing so that he/she can remove the object using the following approach:
// 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;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//perform similar delete action as above but for one cell
XMPPUserCoreDataStorageObject *user = [[self fetchedResultsController] objectAtIndexPath:indexPath];
NSLog(#"User delete: %#", [user displayName]);
//delete from fetchController
NSArray *sections = [[self fetchedResultsController] sections];
int userStatus = [[user sectionNum] intValue];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
But when I do that there is an exception because I am not updating the model which is my fetchedResultsController. The question is how to remove from the fetchedController using the indexPath that I have from the commitEditingStyle ?
Since you are using NSFetchedResultsController you only need to delete the object from the context, and it will pick up the change and delete the rows for you.
So remove the deleteRowsAtIndexPaths: line and call this instead:
[self.fetchedResultsController.managedObjectContext deleteObject:user];
Have a look at this Answer , what you have to do is delete an NSManagedObject from your database as well as from the NSFetchedResultsController.

push next view with didSelectRowAtIndexPath

I added this method didSelectRowAtIndexPath to go to the next view
no errors but i can't go to the next view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
detailhadith *detaihd = [[detailhadith alloc] initWithNibName:#"detailhadith" bundle:nil];
[self.navigationController pushViewController:detaihd animated:YES];
[detaihd release];
}
you can just add a subview like this: [self.view addSubview:nextView.view]; if you don't have a navigationController.