2014-02-23 27 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