简体   繁体   中英

Cell selection and transfer his data to another tableview

Please someone,
I have two Tableview.
In tv1 possible to create cells, then the cells have a segue to another vc where you can see the details of the cell.
In 2tv, I have a button in navitaionControllr, (+), and is opens up the Tv1.
What I'm trying to do is click on the cell from tv1 ֿ and transfer data cell to tv2
And I can not do it.

I used the function:

- (Void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath

I put it in VC1 and that its code:

- (Void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath
{
    if ([self.getFromManagerWorkerViewController isEqualToString: @ "yes"])
    {
        NSManagedObject * Workers = [self.Workers objectAtIndex: indexPath.row];
        ManagerWorker * m = [[ManagerWorker alloc] init];
        
        
        m.ListWorkerArray = [[NSMutableArray alloc] initWithObjects: [Workers valueForKey: @ "lastname"], [Workers valueForKey: @ "firstname"], [Workers valueForKey: @ "title"], [Workers valueForKey: @ "image"] , nil];
        
        self.getFromManagerWorkerViewController = [NSMutableString stringWithFormat: @ "no"];
        [self.navigationController popViewControllerAnimated: YES];
}

As you can see, I have in Vc2, an array called ListWorker
All the information I need from the cell, I chose to enter the system.
But when the function ends left empty array, and it's not clear why ...

i using on core data...

Try writing a delegate for tv1, which will pass the cell in the didSelectRowAtIndexPath method:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ...code...
    if(isOpenFromDetailVC) {
        [self.delegate tableView:tableView wantsToPassCell:cell];
    }
    ...code...
}

Just remember to set the detailVC or even tv2 as the delegate of tv1. Then in the implementation method of the protocol, try adding the cell into tv2.

You can try doing this by eg. adding the cells to an array in the protocol implementation method, and then loading them in tv2 int the cellForRowAtIndexPath method.

If you do not know delegates, check apple docs and this topic:

How do I set up a simple delegate to communicate between two view controllers?

Here is the example shown under the link:

Simple example...

Let's say the child view controller has a UISlider and we want to pass the value of the slider back to the parent via a delegate.

In the child view controller's header file, declare the delegate type and its methods:

 ChildViewController.h #import <UIKit/UIKit.h> // 1. Forward declaration of ChildViewControllerDelegate - this just declares // that a ChildViewControllerDelegate type exists so that we can use it // later. @protocol ChildViewControllerDelegate; // 2. Declaration of the view controller class, as usual @interface ChildViewController : UIViewController // Delegate properties should always be weak references // See https://stackoverflow.com/a/4796131/263871 for the rationale // (Tip: If you're not using ARC, use `assign` instead of `weak`) @property (nonatomic, weak) id<ChildViewControllerDelegate> delegate; // A simple IBAction method that I'll associate with a close button in // the UI. We'll call the delegate's childViewController:didChooseValue: // method inside this handler. - (IBAction)handleCloseButton:(id)sender; @end // 3. Definition of the delegate's interface @protocol ChildViewControllerDelegate <NSObject> - (void)childViewController:(ChildViewController*)viewController didChooseValue:(CGFloat)value; @end 

In the child view controller's implementation, call the delegate methods as required.

Child

 ViewController.m #import "ChildViewController.h" @implementation ChildViewController - (void)handleCloseButton:(id)sender { // Xcode will complain if we access a weak property more than // once here, since it could in theory be nilled between accesses // leading to unpredictable results. So we'll start by taking // a local, strong reference to the delegate. id<ChildViewControllerDelegate> strongDelegate = self.delegate; // Our delegate method is optional, so we should // check that the delegate implements it if ([strongDelegate respondsToSelector:@selector(childViewController:didChooseValue:)]) { [strongDelegate childViewController:self didChooseValue:self.slider.value]; } } @end 

In the parent view controller's header file, declare that it implements the ChildViewControllerDelegate protocol.

 RootViewController.h #import <UIKit/UIKit.h> #import "ChildViewController.h" @interface RootViewController : UITableViewController <ChildViewControllerDelegate> @end 

In the parent view controller's implementation, implement the delegate methods appropriately.

 RootViewController.m #import "RootViewController.h" @implementation RootViewController - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { ChildViewController *detailViewController = [[ChildViewController alloc] init]; // Assign self as the delegate for the child view controller detailViewController.delegate = self; [self.navigationController pushViewController:detailViewController animated:YES]; } // Implement the delegate methods for ChildViewControllerDelegate - (void)childViewController:(ChildViewController *)viewController didChooseValue:(CGFloat)value { // Do something with value... // ...then dismiss the child view controller [self.navigationController popViewControllerAnimated:YES]; } @end 

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM