iphone - UITableViewController 不调用委托(delegate)方法

标签 iphone ios delegates uitableview

<分区>

我想在弹出窗口中显示一个tableViewController,像这样创建TableViewController。

@interface ContentViewController :UITableViewController{

}
@end

@implementation ContentViewController

- (id)initWithStyle:(UITableViewStyle)style {
    if ((self = [super initWithStyle:style])) {
        self.contentSizeForViewInPopover = CGSizeMake(100,400);

    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return 10;
}



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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
cell.textLabel.text = [NSString stringWithFormat:@"Item %d", [indexPath row]]; 
cell.textLabel.textColor = [UIColor whiteColor];
    return cell;
}

从另一个 ViewController 中,我在 popOver 中调用此 tableViewController,如下所示。

UIViewController *contentViewController = [[ContentViewController alloc]    initWithStyle:UITableViewStylePlain];
self.popoverController = [[popoverClass alloc]    initWithContentViewController:contentViewController] ;
self.popoverController.delegate = self;
CGRect rect = btn.frame;
[self.popoverController presentPopoverFromRect:rect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

当我使用 init 语句分配 ContentViewController 时,tableView 委托(delegate)方法不会调用,并且在 popOver 中不会创建任何内容。

如何调用此委托(delegate)方法。

最佳答案

检查您的委托(delegate)和数据源的分配位置。

如果您的 ContentViewController 有 nib 文件方式,只需跟踪数据源并委托(delegate)给您的文件所有者。否则,通过设置委托(delegate)和数据源来覆盖您的 initWithStyle 方法,如下所示。

self.tableView.delegate = self;
self.tableView.datasource = self;

并告诉 contentViewController 将实现 tableview Controller 委托(delegate)和数据源方法,如

@interface ContentViewController :UITableViewController<UITableViewDelegate, UITableViewDataSource>{

}
@end

关于iphone - UITableViewController 不调用委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15810938/

上一篇:ios - UIAlertView 与 UIAlertViewStyleSecureTextInput : clearing field after regain activity

下一篇:iphone - 如何使 View 像揉皱的纸一样动画

相关文章:

ios - HKObserverQuery无法在iPhone上运行

ios - 在 Cocos2d 中的 Sprites 制作的两点之间绘制一条线 Sprite

ios - 我可以将委托(delegate)链接在一起吗?

c# - 在 Silverlight 中同步操作

java - Android - 当事件发生在不同的类上时得到通知

ios - % 字符未显示在 iOS 通知中

iphone - didRangeBeaconsInRegion 代表继续调用 iBeacon iPhone sdk

ios - 为各种函数创建一个数组线程安全 - iOS

ios - Obj-c 协议(protocol)属性未在符合类中实现

iphone - NSMutableDictionary EXC_BAD_ACCESS