无法在UITableView实现中调用委托/数据源方法 [英] Fails to call delegate/datasource methods in UITableView implementation

查看:80
本文介绍了无法在UITableView实现中调用委托/数据源方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为 UITableView 创建了.h和.m文件,名为 mainTableViewgm.h mainTableViewgm.m resp。我正在调用 - initWithFrame :从我的主视图控制器到此 mainTableViewgm.m 实现文件的方法

I have created .h and .m files for UITableView called mainTableViewgm.h and mainTableViewgm.m resp. and I am calling -initWithFrame: method from my main view controller to this mainTableViewgm.m implementation file

[[mainTableViewgm alloc]initWithFrame:tableViewOne.frame]

请注意,此tableview位于我的主视图控制器中。但我已经为tableView创建了单独的文件,并且还在故事板中将自定义类设置为 mainTableViewgm

Note that this tableview is in my main view controller. But I have created separate files for the tableView and have also set the custom class to mainTableViewgm in storyboard.

-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;
}

-initWithFrame:和此方法中的'if(self)'块一起被称为罚款。但问题是 numberOfRowsInSection: cellForRowAtIndexPath:这里没有自动调用。 kource数据 / kource data2 永远不会出现在日志中。如何加载表格? 委托 / 数据源设置不正确吗?

the -initWithFrame: is being called fine along with the 'if (self)' block in this method. But the problem is numberOfRowsInSection: and cellForRowAtIndexPath: are not being automatically called here . kource data/kource data2 never appear in log. What do I do to load the table? Are the delegate/datasource being set incorrectly?

我必须提一下,我还设置了 UITableViewDelegate UITableviewDataSource 协议:

I must mention that I have also set the UITableViewDelegate and UITableviewDataSource protocols:

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

非常感谢帮助。谢谢。

推荐答案

我简直不敢相信我已经做了两年的XCode编程,但仍然遇到了这个问题。

I can't believe I've been doing XCode programming for two years, and still hit this issue.

我遇到了与XCode 6.1相同的问题 - 我正在设置我的 UITableView 的委托& viewWillAppear 函数中的dataSource,但没有任何委托函数正在启动。

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.

但是,如果我对 - 单击Storyboard上的 UITableView ,委托和dataSource的圆圈为空。

However, if I right-clicked on the UITableView on the Storyboard, the circles for delegate and dataSource were empty.

解决方案,然后,是按住CTRL键,并从每个圆圈拖动到 UIView 的名称,其中包含 UITableView

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:

执行此操作后,我的 UITableView 自行填充。

After doing this, my UITableView happily populated itself.

(那么,我们现在是XCode的v6.1版本吗?你认为Apple会做出这件事,你知道吗,友好 ......?我很想添加一个在我的代码中添加书签......这将是一个不错的功能。)

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

这篇关于无法在UITableView实现中调用委托/数据源方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆