繁体   English   中英

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

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

拜托某人
我有两个Tableview。
在可能的tv1中创建单元的情况下,这些单元将与另一个vc相连,您可以在其中看到该单元的详细信息。
在2tv中,我在navitaionControllr中有一个按钮(+),它打开了Tv1。
我想做的是单击tv1 the中的单元,然后将数据单元传输到tv2
而且我做不到。

我使用的功能:

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

我把它放在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];
}

如您所见,我在Vc2中有一个名为ListWorker的数组
我需要从该单元格获取的所有信息,因此我选择进入系统。
但是当函数结束时留空数组,并且不清楚为什么...

我在核心数据上使用...

尝试为tv1编写一个委托,该委托将在didSelectRowAtIndexPath方法中传递单元格:

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

只要记住将detailVC甚至tv2设置为tv1的委托即可。 然后在协议的实现方法中,尝试将单元添加到tv2中。

您可以尝试通过例如这样做。 使用协议实现方法将单元格添加到数组中,然后将它们加载到tv2中,并将其加载到cellForRowAtIndexPath方法中。

如果您不知道委托,请检查Apple文档和以下主题:

如何设置一个简单的委托以在两个视图控制器之间进行通信?

这是链接下方显示的示例:

简单的例子...

假设子视图控制器有一个UISlider,我们想通过委托将滑块的值传递回父级。

在子视图控制器的头文件中,声明委托类型及其方法:

 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 

在子视图控制器的实现中,根据需要调用委托方法。

儿童

 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 

在父视图控制器的头文件中,声明它实现了ChildViewControllerDelegate协议。

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

在父视图控制器的实现中,适当地实现委托方法。

 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 

希望这可以帮助!

暂无
暂无

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

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