开发者

Problem with secondary delegate

I have two UITableView, each one has a different delegate in the same UIViewController.

I'm trying to call control in another delegate, but it failed.

Brief:

mainViewController contains UITableView (with UITableViewDelegate and UITableViewDataSource) + UIImageView.

secondaryTable contains UITableView (with UITableViewDelegate and UITableViewDataSource).

I want to call UIImageView from secondaryTable.

In .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

In .m

@implementation secondaryTable

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sec开发者_运维知识库tion {
    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 =@"A";
    if (indexPath.row == 1) cell.textLabel.text =@"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
}

It types "Selected" in console log, but it won't show control.

Why can't it call navbar control in mainViewController?


I would suggest doing this by having both tables point to the same delegate but, within your delegate methods, select behaviour based on which table it is. Example:

- (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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜