0

I have a question, I have aUITableViewController which have a list with something, When I push on a row it send me to anotherUIViewController.

I implemented this with prepareForSegue method also I put

- (void)tableView:(UITableView *)tableView commitEditingStyle:

when I swipe a row is deleting. Everything is working perfect.

Now, my question is, I put a UIButton inUIViewController, and I want when I push on that to delete the row fromUITableViewController, how to do this?

ZeMoon
  • 20,054
  • 5
  • 57
  • 98
Dan Paschevici
  • 961
  • 2
  • 10
  • 22

2 Answers2

2

You can use delegates, as you want to communicate between two view controllers.

Create a protocol in DetailViewController. While you initially segue from TableViewController to DetailViewController set "idx" as the selected indexPath.row or array index.

When you delete it from DetailViewController, delegate will send the index to the TableViewController and you can remove it from your main array there.

DetailViewController.h file

    #import <UIKit/UIKit.h>


  @protocol DetailViewControllerDelegate <NSObject>

  @optional
  -(void) removeElementAt:(int )index;
  @end

  @interface DetailViewController : UIViewController{

        IBOutlet UIButton *bttn;
        id <DetailViewControllerDelegate> delegate;

    }

    @property (retain) int idx;

    @property (retain) id delegate;

    -(IBAction)bttnclicked;
    -(IBAction)back:(id)sender;

    @end

In DetailViewController.m file

 #import "DetailViewController.h"
    #import "TAbleViewController.h"

    @interface DetailViewController ()

    @end

    @implementation DetailViewController

    @synthesize idx,delegate;

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }



          - (void)viewDidLoad
            {
                [super viewDidLoad];
                // Do any additional setup after loading the view from its nib.
            }

            - (void)didReceiveMemoryWarning
            {
                [super didReceiveMemoryWarning];
                // Dispose of any resources that can be recreated.
            }

            -(IBAction)bttnclicked{
               [[self delegate] removeElementAt:idx];
            }

            -(IBAction)back:(id)sender{
                [self dismissViewControllerAnimated:YES completion:NULL];
            }

            @end

For TableViewContoller.h file

 #import <UIKit/UIKit.h>
   #import "DetailViewController.h"

    @interface TableViewContoller : UIViewController <DetailViewControllerDelegate>
    {
        DetailViewController *secondview;

    }

    @end

In TableViewController.m file,

   #import "TableViewController.h"
   #import "DetailViewController.h"

   @interface TableViewController ()

                @end

                @implementation TableViewController

                - (void)viewDidLoad
                {
                    [super viewDidLoad];
                    // Do any additional setup after loading the view, typically from a nib.
                }

                - (void)didReceiveMemoryWarning
                {
                    [super didReceiveMemoryWarning];
                    // Dispose of any resources that can be recreated.
                }
                  -(void) removeElementAt:(int )index
            {
                NSLog(@"Before object : %@",self.objects);

                [self.objects removeObjectAtIndex:index];
                //reload table at your convinience
                NSLog(@"Removed object : %@",self.objects);

                [self.tableView reloadData];
            }


       - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        if ([[segue identifier] isEqualToString:@"showDetail"]) {
            NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
            DetailViewController *dc=[segue destinationViewController];
            dc.idx = (int) indexPath.row;
            dc.delegate = self;

        }
    }
Jen Jose
  • 3,995
  • 2
  • 19
  • 36
0

The commitEditingStyle: adjusts the core data source of what you're viewing. The delete button should adjust the core data the same way.

Look at Apple's Documentation for creating and deleting managed objects.

If you're deleting the data the row is represented by and still viewing the row when you push back to the UITableViewController, then you need to refresh the data.

Andrew T.
  • 2,088
  • 1
  • 20
  • 42
  • No, the commitEditingStyle is working fine, I have two viewControllers, first is UITableView, second is UIViewController, in second I have button, and I want that when I push on row to delete the row that I selected from firstViewController and push back to tableList. – Dan Paschevici May 13 '15 at 13:04
  • I still don't quite understand your grammar but I'm assuming you want to delete the data object that the UIViewController is displaying. `commitEditingStyle` does this in the background. That was what I was saying. Your delete UIButton needs to delete the object that your UIViewController is displaying from the UIManagedObjectContext. `[aContext deleteObject:aManagedObject];` will do that. This is assuming you are using CoreData for your app. – Andrew T. May 13 '15 at 13:10
  • Where in the UITableViewController are you refreshing your displayed data? – Andrew T. May 13 '15 at 13:10
  • in viewWillAppear I put a [self.tableView reloadData]; – Dan Paschevici May 13 '15 at 13:17
  • Make sure you are refreshing on the main queue and that you are committing your deletion to the object context before refreshing. http://stackoverflow.com/questions/24939434/self-tableview-reloaddata-not-working-after-successful-call-in-afnetworking – Andrew T. May 13 '15 at 13:20