2011-09-26 54 views
1

我有兩個UITableView,每個人在同一個UIViewController中都有一個不同的委託。第二級代理問題

我想在另一個委託中調用控件,但失敗了。

Brief:
mainViewController包含UITableView(帶有UITableViewDelegate和UITableViewDataSource)+ UIImageView。
secondaryTable包含UITableView(帶有UITableViewDelegate和UITableViewDataSource)。

我想從secondaryTable調用UIImageView。

在.H

@interface secondaryTable : UIViewController <UITableViewDataSource,UITableViewDelegate> 
@end 


@interface mainViewController : UIViewController <UITableViewDataSource,UITableViewDelegate> { 

@property (nonatomic,retain) IBOutlet UIImageView *navbar; 
@property (nonatomic,retain) IBOutlet UITableView *Table1; 
@property (nonatomic,retain) IBOutlet UITableView *Table2; 
@end 

在.M在控制檯日誌

@implementation secondaryTable 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return 2; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *MyIdentifier = @"MyIdentifier2"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
    if (cell == nil) { 

     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease]; 

     [cell.textLabel setFont:[UIFont boldSystemFontOfSize:12.0]]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     cell.textLabel.textAlignment = UITextAlignmentCenter; 
    } 


    if (indexPath.row == 0) cell.textLabel.text [email protected]"A"; 
    if (indexPath.row == 1) cell.textLabel.text [email protected]"B"; 

    return cell; 

} 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    NSLog(@"Selected"); 

    mainViewController *myController = [[mainViewController alloc] init]; 

    [myController.navbar setHidden:NO]; 
} 

@end 


@implementation mainViewController 

- (void)viewDidLoad { 
Table1.delegate = self; 
Table1.dataSource = self; 

secondaryTable *myDelegate = [secondaryTable alloc]; 
Table2.delegate = myDelegate; 
Table2.dataSource = myDelegate; 
@end 
} 

這類型 「選擇」,但它不會顯示控制。
爲什麼不能在mainViewController中調用navbar控件?

回答

1

我會建議通過讓這兩個表指向同一個委託來完成此操作,但是,在您的委託方法中,根據它是哪個表來選擇行爲。例如:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (tableView == firstTable) 
    { 
     // Do appropriate things here 
    } 
    else if (tableView == secondTable) 
    { 
     // do things for table #2 here 
    } 
    else 
    { 
     assert(no); // error checking 
    } 
} 
+0

我試過之前,它沒有工作。 numberOfRowsInSection無法識別tableview – Houranis

+0

ADD:我使用UIViewController而不是UITableViewController。 – Houranis

+0

如果你的UIViewController正確地實現了' - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section',那麼你會得到有問題的tableView。如果您遇到問題,那幾乎肯定是您的代碼中的錯誤,而不是SDK。這適用於目前正在運送的應用程序的數千種 - 相信我! :) – Olie