2014-02-23 28 views
1

新作为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 

非常感谢。

+0

您声称您使用的代表在哪里?我没有看到任何。 –

+0

我以为我使用委托,使用这个:http:// stackoverflow。com/questions/14647283/how-to-listen-for-a-didselectropowatindexpath-change-in-another-view-controller – David

+0

不,这不是委托。我会为您提供一种方法来做到这一点。 –

回答

2

LeftViewController.h设置委托:

@protocol LeftViewControllerDelegate <NSObject> 

// methods 
@required 
- (void)didSelectRow:(NSUInteger)selectedIndex; 
@end 

@interface LeftViewController : UITableViewController{ 
    id <LeftViewControllerDelegate> delegate; 
} 

@property (retain) id delegate; 

// the rest you already have... 

LeftViewController.m

#import "RightViewController.h" 
@synthesize delegate; 

in the didSelectRowAtIndexPath function: 

RightViewController *rightController = [[RightViewController alloc]init]; 
delegate = rightController; 

if([self.delegate respondsToSelector:@selector(didSelectRow:)]) { 
    //send the delegate function with the amount entered by the user 
    [self.delegate didSelectRow:indexPath]; 
} 

然后,你必须准备RightViewController

首先添加代理(RightViewController.h ):

@interface RightViewController : UITableViewController <LeftViewControllerDelegate, UITableViewDataSource, UITableViewDelegate> 

,并在您RightViewController.m,在viewDidLoad中关闭此:

[vc1 addObserver:self forKeyPath:@"selectedIndexPath" options:NSKeyValueObservingOptionNew context:NULL]; 

到这一点:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ReloadDataFunction:) name:@"refresh" object:nil]; 

和功能

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 

到这一点:

-(void)ReloadDataFunction:(NSNotification *)notification 

并添加didSelectRow功能为代表的东西:

- (void)didSelectRow:(NSUInteger)bookIndex 
{ 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"refresh" object:self userInfo:nil]; 
} 

而且代码:

int totalCountries = 10; 
for(int x = 1; x <= totalCountries; x++) { 
    [countries addObject:[NSMutableString stringWithFormat:@"%@",[myCountries objectAtIndex:x]]]; 
} 

也许将你的程序崩溃,因为首先你有3个国家的NSArray的* myCountries和你迭代10次,然后你从x = 1开始,尽管国家数组也有索引0.用这个代替:

int totalCountries = 3; 
for(int x = 0; x < totalCountries; x++) { 
    [countries addObject:[NSMutableString stringWithFormat:@"%@",[myCountries objectAtIndex:x]]]; 
} 
+0

谢谢,工作!如果我想通过右侧或左侧控制器更新MainViewController,怎么办?我必须做同样的事情? – David

+0

是的,通过在MainViewController的'viewDidLoad'中声明一个通知回调函数来做同样的事情。 –

+0

谢谢,找到了方法! – David