将数据从动态tableview传递回静态tableview

时间:2013-12-02 17:08:15

标签: ios objective-c uitableview

请帮助我一直在努力回传数据。我有2个tableViews。 1st tableview = static table = RootVC。 2nd tableview = dynamic table = FirstVC。在RootVC中,我有一个带有两个标签的单元格,“repeatLabel”和“repeatDetail”,带有披露指示符。当我点击单元格时,它显示下一个表格,即FirstVC,FistVC填充了工作日。选择我的选择之后,我希望选择的日期在“repeatDetail”中传回RootVC,当我返回时仍然能够看到以前选择的数据。

我的RootVC看起来像这样:

    #import "RepeatViewController.h"
    @interface SettingsViewController : UITableViewController
    @property (strong, nonatomic) IBOutlet UILabel *repeatDetail;
    @property (strong, nonatomic) IBOutlet UILabel *repeatLabel;
    @property (strong,nonatomic) NSString *getRepeatDetail;
    @property (nonatomic, strong) NSMutableArray *selectedDaysArray;
    @end


    in my RootVC.m
    #import "SettingsViewController.h"

    @interface SettingsViewController ()

    @end

    @implementation SettingsViewController
    @synthesize repeatLabel,repeatDetail;
    @synthesize getRepeatLabel;
    @synthesize selectedDaysArray;
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        repeatLabel.text = @"Repeat";
        repeatDetail.text = getRepeatLabel;



    }

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



     - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        RepeatViewController *destinationController = segue.destinationViewController;
        if( [destinationController isKindOfClass:[RepeatViewController class]] )
        {
            //You can reuse your selectedDays arrays
            destinationController.selectedDays = self.selectedDaysArray;
            [(RepeatViewController *)destinationController setCompletionBlock:^(NSArray *retDaysArray) // <- make this change
             {


                 // Save your changes
                 self.selectedDaysArray = [NSMutableArray arrayWithArray: retDaysArray]; // <- make this change

                 NSLog(@"retDaysArray: %@", self.selectedDaysArray); //<- Add this debug line
             }];
        }
    }


    @end

    My 1stVC.h

    #import "SettingsViewController.h"
    typedef void(^WeekdayCompletionBlock)(NSArray *retDaysArray);
    @interface RepeatViewController : UITableViewController <UITableViewDataSource,UITableViewDelegate>

    @property (nonatomic,strong) NSMutableArray *selectedDays;
    @property (nonatomic, copy) NSArray *completionBlock;
    @property (copy) WeekdayCompletionBlock returnBlock;
    //@property (strong, nonatomic) IBOutlet UIBarButtonItem *saveButton;
    -(IBAction)save:(id)sender;
    @end

    my 1stVC.m

    #import "RepeatViewController.h"

    @interface RepeatViewController ()

    @end

    @implementation RepeatViewController
    @synthesize selectedDays= _selectedDays;
    @synthesize completionBlock;
    @synthesize returnBlock;
    - (id)initWithStyle:(UITableViewStyle)style
    {
        self = [super initWithStyle:style];
        if (self) {
            // Custom initialization
        }
        return self;
    }

    - (void)viewDidLoad
    {
        [super viewDidLoad];

        completionBlock = [NSArray arrayWithObjects:@"Sunday", @"Monday", @"Tuesday", @"Wednesday", @"Thursday", @"Friday", @"Saturday", nil];


    }

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

    #pragma mark - Table view data source

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        // Return the number of sections.
        return 1;
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        // Return the number of rows in the section.
        return 7;
    }



     - (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];


        }
        NSString *day = completionBlock[indexPath.row];
        cell.textLabel.text = day;
        if ([self.selectedDays containsObject:day])
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        else
            cell.accessoryType = UITableViewCellAccessoryNone;


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

        return cell;
    }

    #pragma mark - Table view delegate


    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if (!self.selectedDays)
            self.selectedDays = [[NSMutableArray alloc] init];
        if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
            //remove data from array
            [self.selectedDays removeObject:[completionBlock objectAtIndex:indexPath.row]];
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
            //add data to array
            [self.selectedDays addObject:[completionBlock objectAtIndex:indexPath.row]];

        }

        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }


    -(IBAction)save:(id)sender
{
    NSUserDefaults *myNewWeekString = [NSUserDefaults standardUserDefaults];
    [myNewWeekString setObject:self.selectedDays forKey:@"MY_KEY_FOR_ACCESING_DAYSOFWEEK"];
    [myNewWeekString synchronize];

    //NSLog(@"The selected day/s is %@",self.selectedDays);

    if (self.returnBlock)
    {
        self.returnBlock(self.selectedDays);
    }
    [self.navigationController popViewControllerAnimated:YES];
   // NSLog(@"The selected day/s is %@",self.selectedDays);
   // if (self.returnBlock)
   // {
      //  self.returnBlock([completionBlock objectAtIndex:indexPath.row]);
    //}

}



      /*
    -(void) setReturnBlock:(WeekdayCompletionBlock)returnBlock
    {
        [self.selectedDays addObject:(self.returnArray);
    }

    - (NSArray *)setDats
    {
        return [NSArray arrayWithArray:[self.selectedDays copy]];
    }*/




    @end

2 个答案:

答案 0 :(得分:0)

当你使用静态单元格时,你必须直接绑定你正在使用的控件,没有必要。

所以我建议你如下:

  1. 使用某些特定标识符绑定控件,例如labelFieldRow {rowid}示例:labelFieldRow1。
  2. 因此,在准备segue时,只需检查所选行的内容并将所需数据传递给目标控制器。
  3. 可能不是最好的,但应该有效。

答案 1 :(得分:0)

您必须将数据(之前由用户选择)从RootVC传递到FirstVC。要在RootVC添加属性中执行此操作以保留选定的数据;

@property (nonatomic, strong) NSMutableArray *selectedDaysArray;

在prepareForSegue方法中,您必须传递该数组以使表视图知道需要选择的内容:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        UIViewController *destinationController = segue.destinationViewController;
        if( [destinationController isKindOfClass:[RepeatViewController class]] )
        {
            //You can reuse your selectedDays arrays
             ((RepeatViewController*)destinationController).selectedDays = self.selectedDaysArray;
            [(RepeatViewController *)destinationController setReturnBlock:^(NSArray *retDaysArray) // <- make this change
             {
                 // Save your changes
                 self.selectedDaysArray = [NSMutableArray arrayWithArray: retDaysArray]; // <- make this change
                 NSLog(@"DATA: %@", self.selectedDaysArray) //<- Add this debug line
             }];
        }
    }

从viewDidLoad中删除此行,您不想每次都从rootVC传递它时分配它

_selectedDays = [[NSMutableArray alloc] init];

在cellForRowInIndexPath中替换此行:

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

使用此代码:

    NSString *day = completionBlock[indexPath.row];
    cell.textLabel.text = day;
    if ([self.selectedDays containsObject:day])
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    else
        cell.accessoryType = UITableViewCellAccessoryNone;

并将didSelectRowAtIndexPath:更改为

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!self.selectedDays)
        self.selectedDays = [[NSMutableArray alloc] init];
    if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
        //remove data from array
        [self.selectedDays removeObject:[completionBlock objectAtIndex:indexPath.row]];
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        //add data to array
        [self.selectedDays addObject:[completionBlock objectAtIndex:indexPath.row]];
    }

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

希望得到这个帮助。