简体   繁体   English

无法在UITableView实现中调用委托/数据源方法

[英]Fails to call delegate/datasource methods in UITableView implementation

I have created .h and .m files for UITableView called mainTableViewgm.h and mainTableViewgm.m resp. 我为UITableView创建了.h和.m文件,分别称为mainTableViewgm.hmainTableViewgm.m and I am calling - initWithFrame : method from my main view controller to this mainTableViewgm.m implementation file 和我打电话- initWithFrame :方法从我的主视图控制器此mainTableViewgm.m实现文件

[[mainTableViewgm alloc]initWithFrame:tableViewOne.frame]

Note that this tableview is in my main view controller. 请注意,此tableview在我的主视图控制器中。 But I have created separate files for the tableView and have also set the custom class to mainTableViewgm in storyboard. 但是我为tableView创建了单独的文件,并且还在故事板上将自定义类设置为mainTableViewgm

the -initWithFrame: methods appears as follows -initWithFrame:方法如下所示

- (id)initWithFrame:(CGRect)frame
{

//NSLog(@"kource data");
self = [super initWithFrame:frame];
if (self)
{
    [self setDelegate:self];
    [self setDataSource:self];
    [self tableView:self cellForRowAtIndexPath:0];
    [self tableView:self numberOfRowsInSection:1];
    // Initialization code
}

return self;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"kource data");
return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath
{
NSLog(@"kource data2");
UITableViewCell*cellOne =[[UITableViewCell alloc]init];
cellOne.detailTextLabel.text=@"text did appear";
return cellOne;
}

the -initWithFrame: is being called fine along with the 'if (self)' block in this method. -initWithFrame:在此方法中与“ if(self)”块一起被很好地调用。 But the problem is numberOfRowsInSection: and cellForRowAtIndexPath: are not being automatically called here . 但是问题是numberOfRowsInSection:cellForRowAtIndexPath:没有在这里自动调用。 kource data / kource data2 never appear in log. kource data / kource data2永远不会出现在日志中。 What do I do to load the table? 我要如何装载桌子? Are the delegate / datasource being set incorrectly? delegate / datasource设置不正确吗?

I must mention that I have also set the UITableViewDelegate and UITableviewDataSource protocols: 我必须提到,我还设置了UITableViewDelegateUITableviewDataSource协议:

@interface mainTableViewgm : UITableView <UITableViewDelegate,UITableViewDataSource>
@end

Help will be much appreciated. 帮助将不胜感激。 Thank you. 谢谢。

Your tableview is not loaded when the controller is initializing, so you cannot do that in the init methods. 控制器初始化时不会加载tableview,因此您不能在init方法中这样做。 You have to move your code to the viewDidLoad method. 您必须将代码移至viewDidLoad方法。

Also you are not setting the delegate and datasource on the tableview object (probably a type, you are setting them on the view controller). 另外,您没有在tableview对象上设置委托和数据源(可能是类型,而是在视图控制器上设置它们)。 It should look like this: 它看起来应该像这样:

- (void)viewDidLoad:(BOOL)animated {
    [super viewDidLoad:animated];
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self]; // <- This will trigger the tableview to (re)load it's data
}

Next thing is to implement the UITableViewDataSource methods correctly. 下一步是正确实现UITableViewDataSource方法。 UITableViewCell *cellOne =[[UITableViewCell alloc] init]; is not returning a valid cell object. 没有返回有效的单元格对象。 You should use at least initWithStyle: . 您至少应使用initWithStyle: And take a look how to use dequeueReusableCellWithIdentifier:forIndexPath: . 看看如何使用dequeueReusableCellWithIdentifier:forIndexPath: A typical implementation would look like this: 典型的实现如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    // Reuse/create cell    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Update cell contents
    cell.textLabel.text = @"Your text here";
    cell.detailTextLabel.text=@"text did appear";

    return cell;
}

I can't believe I've been doing XCode programming for two years, and still hit this issue. 我不敢相信我从事XCode编程已经两年了,仍然遇到了这个问题。

I had the same problem with XCode 6.1 - I was setting my UITableView 's delegate & dataSource in the viewWillAppear function, but none of the delegate functions were kicking in. 我在XCode 6.1中遇到了同样的问题-我在viewWillAppear函数中设置了UITableView的委托和数据源,但是没有一个委托函数在起作用。

However, if I right-clicked on the UITableView on the Storyboard, the circles for delegate and dataSource were empty. 但是,如果右键单击情节提要上的UITableView ,则代表和dataSource的圆圈为空。

The solution, then, is to hold down the CTRL key, and drag from each of these circles up to the name of your UIView which contains your UITableView : 然后,解决方案是按住CTRL键,并从每个圆圈中拖动到包含UITableView UIView的名称:

在此处输入图片说明

After doing this, my UITableView happily populated itself. 完成此操作后,我的UITableView愉快地填充自身。

(So, we're upto v6.1 of XCode now are we ? Do you think Apple ever going to make this thing, you know, friendly ...? I would quite like to add a Bookmark in my code... that'd be a nice feature.) (所以,现在我们的XCode达到v6.1了吗?您认为Apple会做到这一点吗,您知道吗, 友好的吗??我想在我的代码中添加一个书签...会是一个不错的功能。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM