【问题标题】:Fails to call delegate/datasource methods in UITableView implementation在 UITableView 实现中调用委托/数据源方法失败
【发布时间】:2013-11-18 15:00:11
【问题描述】:

我为 UITableView 创建了 .h 和 .m 文件,分别称为 mainTableViewgm.hmainTableViewgm.m。我正在调用 -initWithFrame: 方法从我的主视图控制器到这个 mainTableViewgm.m 实现文件

[[mainTableViewgm alloc]initWithFrame:tableViewOne.frame]

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

-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 data/kource data2 永远不会出现在日志中。我该怎么做才能加载表格? delegate/datasource 是否设置错误?

我必须提到我还设置了UITableViewDelegateUITableviewDataSource 协议:

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

我们将不胜感激。谢谢。

【问题讨论】:

  • 把cellforrow的减速和节数从initwithframe方法中取出再试一下。

标签: ios iphone objective-c uitableview


【解决方案1】:

控制器初始化时不会加载您的 tableview,因此您不能在 init 方法中执行此操作。您必须将代码移动到 viewDidLoad 方法。

您也没有在 tableview 对象上设置委托和数据源(可能是一种类型,您在视图控制器上设置它们)。它应该是这样的:

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

接下来是正确实现 UITableViewDataSource 方法。 UITableViewCell *cellOne =[[UITableViewCell alloc] init]; 未返回有效的单元格对象。您至少应该使用initWithStyle:。并看看如何使用dequeueReusableCellWithIdentifier:forIndexPath:。典型的实现如下所示:

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

【讨论】:

  • 正如我上面提到的,我想在我专门为这个表(mainTableViewgm.m)而不是 viewController 创建的 UITableView 类型 .m 文件中使用委托/数据源方法。
  • 抱歉,没有注意到这一点,但是子类化 UITableView 对我来说听起来是个坏主意。为了让一切正常工作,可能需要做更多的工作。那么为什么需要继承 UITableView 呢?你不能只使用它的数据源和委托来控制表格视图吗?
  • 这是另一个可能对您有所帮助的链接:stackoverflow.com/questions/9144146/…
【解决方案2】:

不敢相信我已经从事 XCode 编程两年了,仍然遇到这个问题。

我在使用 XCode 6.1 时遇到了同样的问题 - 我在 viewWillAppear 函数中设置了我的 UITableView 的委托和数据源,但没有一个委托函数启动。

但是,如果我右键单击 Storyboard 上的 UITableView,则代表和数据源的圆圈是空的。

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

完成此操作后,我的UITableView 愉快地填充了自己。

(那么,我们现在已经升级到 XCode 的 v6.1 了吗?你认为 Apple 会做出这个东西吗,你知道的,友好...?我很想在我的代码中添加书签...这将是一个不错的功能。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多