简体   繁体   English

将数据从动态tableView传递到静态TableView

[英]Passing Data from Dynamic tableView to Static TableView

Hi Let me try to clarify my issue. 您好,让我尝试澄清我的问题。 I have two TableViews, one is static and the other is dynamic. 我有两个TableViews,一个是静态的,另一个是动态的。 The static= RootVC and Dynamic=FirstVC. 静态= RootVC和动态= FirstVC。 In FirstVC i have data that I want to select,save and pass the saved data to a UILabel in RootVC. 在FirstVC中,我要选择,保存并将保存的数据传递到RootVC中的UILabel的数据。 1)When I run my App data is selected however it is not saved or passed to my rooVC. 1)当我运行我的应用程序数据时,但是它没有保存或传递给我的rooVC。 I was using delegates and was advice not to use "delegate" but use "Blocks". 我正在使用委托,建议不要使用“代理”,而应使用“块”。 But still i'm facing the same issue. 但是我仍然面临着同样的问题。 Here is my code: 这是我的代码:

in rootVC.h
#import <UIKit/UIKit.h>

@interface RootViewController : UITableViewController
{

    NSString *getRepeatLabel;
}
@property (strong, nonatomic) IBOutlet UILabel *repeatLabel;
@property (strong, nonatomic) IBOutlet UILabel *repeatDetail;
@property (nonatomic,strong) NSString *getRepeatLabel;
@end

in my rootVC.m

#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController
- (void)viewDidLoad
{
    [super viewDidLoad];

    _repeatLabel.text = @"Repeat";
    _repeatDetail.text = getRepeatLabel;


}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    UIViewController *destinationController = segue.destinationViewController;
    if( [destinationController isKindOfClass:[FirstViewController class]] )
    {
        [(FirstViewController *)destinationController setCompletionBlock:^(NSString *getRepeatLabel;)
         {

             // do something here with your string // maybe you must reload your table // it depends on where your returning data needs to display    <--------Not sure what to do here
             // NSDateFormatter*dateFormatter = [[NSDateFormatter alloc]init];
             //    NSArray*days = [dateFormatter shortWeekdaySymbols];  <------Here I would like when data is selected to show days in short symbol
             NSLog (@"The selected day/s is %@", getRepeatLabel); <---nothing displaying on console




         }];
    }
}

@end

in FirstVC.h

#import <UIKit/UIKit.h>
#import "RootViewController.h"
typedef void(^WeekdayCompletionBlock)(NSString *dayName);
@interface FirstViewController : UITableViewController
{
    NSString *dayName;
}


@property (nonatomic, strong) WeekdayCompletionBlock completionBlock;
@property (nonatomic,strong) NSString *dayName;
- (IBAction)save:(id)sender;
@end

in FirstVC.m

#import "FirstViewController.h"
#import "RootViewController.h"
@interface FirstViewController ()
@end

@implementation FirstViewController
@synthesize completionBlock;
@synthesize dayName;


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // Initialize table data
    completionBlock = [NSArray arrayWithObjects:@"Sunday", @"Monday", @"Tuesday", @"Wednesday", @"Thursday", @"Friday", @"Saturday", nil];


}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations

    return YES;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [completionBlock count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"RepeatCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];


    }


    cell.textLabel.text = [completionBlock objectAtIndex:indexPath.row];

    return cell;
}




// Called after the user changes the selection.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog (@"The selected day/s is %@", [completionBlock objectAtIndex:indexPath.row]);


   _getRepeatLabel = completionBlock; //<-----------string from RootVC gives error "undeclared _getRepeatLabel"
}
- (IBAction)save:(id)sender
{
    NSUserDefaults *myNewWeekString = [NSUserDefaults standardUserDefaults];
    [myNewWeekString setObject:completionBlock forKey:@"%@"];
    [myNewWeekString synchronize];
    self.completionBlock(myNewDayOfWeekString) <------error myNewDayOfWeekString undeclared and if i declare it here it complains about incompatibility


}
@end

Your code is a little bit wrong. 您的代码有点错误。 I think you don't really understand block. 我认为您不太了解阻止。 You want to pass more than string so the best way to do that is via array. 您希望传递的不仅仅是字符串,所以最好的方法是通过数组。 Change block definition to accept array instead of string: 更改块定义以接受数组而不是字符串:

typedef void(^WeekdayCompletionBlock)(NSArray *dayName);

Change declaration of your property in FirstVC.h to: 将您在FirstVC.h中的属性的声明更改为:

@property (nonatomic, copy) NSArray *completionBlock; //This is your array, you use it as data source, It's not a block
//Add your block property
// This is your block property you will use it to pass the data between view controllers
@property (copy) WeekdayCompletionBlock returnBlock;
//Add property to keep your selected days
@property (nonatomic, strong) NSMutableArray *returnArray;

Add this line to viewDidLoad method: 将此行添加到viewDidLoad方法:

self.returnArray = [[NSMutableArray alloc] init];

Change your didSelectRowAtIndexPath method in FirstVC.m to: 在FirstVC.m中将您的didSelectRowAtIndexPath方法更改为:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
        //remove data from array
        [self.returnArray removeObject:[completionBlock objectAtIndex:indexPath.row]];
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        //add data to array
        [self.returnArray addObject:[completionBlock objectAtIndex:indexPath.row]];
    }

   [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

In your save: method call block and pass value to rootVC replace line: 在您的save:方法调用块中并将值传递给rootVC替换行:

self.completionBlock(myNewDayOfWeekString);

with: 与:

    if (self.returnBlock)
    {
        self.returnBlock(self.returnArray);
    }
    [self.navigationController popViewControllerAnimated:YES];

The last change left to do is set up ablok in your rootVC.m file. 剩下要做的最后更改是在rootVC.m文件中设置ablok。 Replace line: 替换行:

[(FirstViewController *)destinationController setCompletionBlock:

with (nsstring needs to be replaced with nsarray - you pass array with all of the selected data) 与(需要用nsarray替换nsstring-您将所有选定数据传递给array)

[(FirstViewController *)destinationController setCompletionBlock:^(NSArray *getRepeatLabel)

You set up block not NSArray. 您设置的不是NSArray。

I don't know what are you trying to do here: 我不知道您要在这里做什么:

[myNewWeekString setObject:completionBlock forKey:@"%@"];

You are using %@ as a key. 您正在使用%@作为密钥。 It should be text for example @"MY_KEY_FOR_ACCESING_DAYSOFWEEK". 它应该是文本,例如@“ MY_KEY_FOR_ACCESING_DAYSOFWEEK”。 You are saving completionBlock it's all of your days if you want to save just selected days replace it with self.returnArray. 您正在保存completeBlock,这是您的所有工作日,如果您只想保存选定的几天,请用self.returnArray替换它。

Hope this help. 希望对您有所帮助。

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

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