单元格选择并将其数据传输到另一个tableview

时间:2015-11-25 11:12:25

标签: ios objective-c

请某人,
我有两个Tableview 在tv1中可以创建单元格,然后单元格与另一个vc有一个segue,您可以在其中看到单元格的详细信息。
在2tv中,我在navitaionControllr(+)中有一个按钮,打开Tv1。
我要做的是点击tv1的单元格并将数据单元格转移到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的数组 我需要从单元格中获取所有信息,我选择进入系统 但是当函数结束时左空数组,并且不清楚为什么......

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

1 个答案:

答案 0 :(得分:0)

尝试为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文档和此主题:

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

以下是链接下显示的示例:

  

简单的例子......

     

让我们说子视图控制器有一个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
     

希望这有帮助!