新作为ios开发者,我在我的应用程序中使用MKDSlideViewController。 而我有一个很大的问题,我无法解决它。在LeftViewController(左视图 - 菜单)中有一个tableView。并在RightViewController
也有一个tableView。现在,我的问题是,虽然我有使用委托,提供解决方案here,我不能通知RightViewController重新加载与左表的选定行关联的表数据(右表)。MKDSlideViewController重新加载右表取决于左表的选定行
我尝试添加图片,但我没有声望,所以我会尝试添加链接。 代码MKDSlideViewController
插件与上面的通知解决方案合并。
的LeftViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
//this will fire the property changed notification
if(indexPath!=self.selectedIndexPath)
self.selectedIndexPath = indexPath;
UINavigationController * centerNavigationController = (UINavigationController *)self.navigationController.slideViewController.mainViewController;
if(row == 0)
{
if([centerNavigationController.topViewController isKindOfClass:[MainViewController class]])
[self.navigationController.slideViewController showMainViewControllerAnimated:YES];
else
{
UIViewController * mainViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"MainViewController"];
[self.navigationController.slideViewController setMainViewController:mainViewController animated:YES];
}
}
else if(row == 1)
{
if([centerNavigationController.topViewController isKindOfClass:[SecondaryViewController class]])
[self.navigationController.slideViewController showMainViewControllerAnimated:YES];
else
{
UIViewController * secondaryViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondaryViewController"];
[self.navigationController.slideViewController setMainViewController:secondaryViewController animated:YES];
}
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
和我RightViewController.m
#import "RightViewController.h"
#import "CountryTableCell.h"
@implementation RightViewController
@synthesize vc1;
@synthesize myTable;
@synthesize countries;
@synthesize countryLabel;
-(void)viewDidLoad
{
[super viewDidLoad];
[vc1 addObserver:self forKeyPath:@"selectedIndexPath" options:NSKeyValueObservingOptionNew context:NULL];
}
-(void)dealloc
{
[myTable release];
[countryLabel release];
[super dealloc];
[vc1 removeObserver:self forKeyPath:@"selectedIndexPath"];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSArray *myCountries = [NSArray arrayWithObjects:@"Germany", @"United Kingdom", @"France", nil];
countries = [NSMutableArray arrayWithObjects:nil];
int totalCountries = 10;
for(int x = 1; x <= totalCountries; x++) {
[countries addObject:[NSMutableString stringWithFormat:@"%@",[myCountries objectAtIndex:x]]];
}
[self.myTable reloadData];
}
- (void)viewDidUnload {
[self setMyTable:nil];
[self setCountryLabel:nil];
[super viewDidUnload];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [countries count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
CountryTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CountryTableCell"];
if(cell == nil){
cell = [[[CountryTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CountryTableCell"] init];
cell.selectionStyle = UITableViewCellEditingStyleNone;
}
cell.countryLabel.text = [countries objectAtIndex:indexPath.row];
return cell;
}
@end
非常感谢。
您声称您使用的代表在哪里?我没有看到任何。 –
我以为我使用委托,使用这个:http:// stackoverflow。com/questions/14647283/how-to-listen-for-a-didselectropowatindexpath-change-in-another-view-controller – David
不,这不是委托。我会为您提供一种方法来做到这一点。 –