簡體   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