Ios 将数据从动态tableview传递回静态tableview
请帮助我,我一直在努力传回数据。我有两个桌面视图。第一个表视图=静态表=RootVC。第二个表格视图=动态表格=第一个VC。在RootVC中,我有一个带有两个标签的单元格,“repeatLabel”和带有披露指示符的“repeatDetail”。当我点击该单元格时,它会显示下一个表FirstVC,FistVC将填充工作日。在选择了我的选择之后,我希望选择的日期以“repeatDetail”的形式传递回RootVC,并且当我返回时仍然能够看到以前选择的数据 我的RootVC如下所示:Ios 将数据从动态tableview传递回静态tableview,ios,objective-c,uitableview,Ios,Objective C,Uitableview,请帮助我,我一直在努力传回数据。我有两个桌面视图。第一个表视图=静态表=RootVC。第二个表格视图=动态表格=第一个VC。在RootVC中,我有一个带有两个标签的单元格,“repeatLabel”和带有披露指示符的“repeatDetail”。当我点击该单元格时,它会显示下一个表FirstVC,FistVC将填充工作日。在选择了我的选择之后,我希望选择的日期以“repeatDetail”的形式传递回RootVC,并且当我返回时仍然能够看到以前选择的数据 我的RootVC如下所示: #i
#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
#导入“RepeatViewController.h”
@接口设置查看控制器:UITableViewController
@属性(强,非原子)IBUILabel*repeatDetail;
@属性(强,非原子)IBUILabel*repeatLabel;
@属性(强,非原子)NSString*getRepeatDetail;
@属性(非原子,强)NSMutableArray*selectedDaysArray;
@结束
在我的RootVC.m
#导入“setingsviewcontroller.h”
@接口设置可视控制器()
@结束
@实现设置可视控制器
@合成repeatLabel,repeatDetail;
@合成重复标签;
@合成选择性daysarray;
-(无效)viewDidLoad
{
[超级视图下载];
repeatLabel.text=@“Repeat”;
repeatDetail.text=getRepeatLabel;
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
-(void)prepareForSegue:(UIStoryboardSegue*)segue发送方:(id)发送方
{
RepeatViewController*destinationController=segue.destinationViewController;
if([destinationController IsKindof类:[RepeatViewController类]])
{
//您可以重复使用selectedDays数组
destinationController.selectedDays=self.selectedDaysArray;
[(RepeatViewController*)destinationController setCompletionBlock:^(NSArray*retDaysArray)//当您使用静态单元格时,您必须直接绑定正在使用的控件,而无需绑定
因此,我可以向您提出以下建议:
使用特定标识符绑定控件,如LabelFieldDrow{rowid}示例:LabelFieldDrow1
因此,在准备segue时,只需检查所选行的内容,并将所需数据传递给目标控制器
可能不是最好的,但它应该可以工作。您必须将数据(以前由用户选择)从您的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
}];
}
}
在CellForRowinIndepath中,替换此行:
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];
}
希望能有所帮助。嗨,卡米洛,我是IOS新手,我如何才能做到这一点,最好的方法是什么?你知道如何将脚本中的控件绑定到你的控制器类吗?很抱歉,我问了一个问题,我该怎么做?我还是个新手,试图理解TableView。答案=不,我不知道怎么做。我发布了答案。让我知道答案是什么e、 嗨,格雷格,它仍然不工作。没有数据被传递到RootVC。我在帖子中添加了调试行。你能添加它并看到吗?你能在控制台中看到输出吗?选择有效吗(它还记得所选的日子吗)?不知道,我已经发布了我所拥有的。没有数据被传递到RootVC查看这一行[(RepeatViewController*)destinationController setReturnBlock:我将其从[(RepeatViewController*)更改为destinationController setCompletionBlock:。如果您还有任何问题,请告诉我。如果我有“UIViewController”,我会收到一个错误-在“UIViewController”类型的对象中找不到属性“selectedDays”
- (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];
}