简体   繁体   English

单元格选择并将他的数据传输到另一个表格视图

[英]Cell selection and transfer his data to another tableview

Please someone, 拜托某人
I have two Tableview. 我有两个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. 在可能的tv1中创建单元的情况下,这些单元将与另一个vc相连,您可以在其中看到该单元的详细信息。
In 2tv, I have a button in navitaionControllr, (+), and is opens up the Tv1. 在2tv中,我在navitaionControllr中有一个按钮(+),它打开了Tv1。
What I'm trying to do is click on the cell from tv1 ֿ and transfer data cell to tv2 我想做的是单击tv1 the中的单元,然后将数据单元传输到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: 我把它放在VC1中,并加上它的代码:

- (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 如您所见,我在Vc2中有一个名为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: 尝试为tv1编写一个委托,该委托将在didSelectRowAtIndexPath方法中传递单元格:

- (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. 只要记住将detailVC甚至tv2设置为tv1的委托即可。 Then in the implementation method of the protocol, try adding the cell into tv2. 然后在协议的实现方法中,尝试将单元添加到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. 使用协议实现方法将单元格添加到数组中,然后将它们加载到tv2中,并将其加载到cellForRowAtIndexPath方法中。

If you do not know delegates, check apple docs and this topic: 如果您不知道委托,请检查Apple文档和以下主题:

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. 假设子视图控制器有一个UISlider,我们想通过委托将滑块的值传递回父级。

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. 在父视图控制器的头文件中,声明它实现了ChildViewControllerDelegate协议。

 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! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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