新作爲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
不,這不是委託。我會爲您提供一種方法來做到這一點。 –