热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

不会调用UITableView委托和dataSource方法-UITableViewdelegateanddataSourcemethodsnotgettingcalled

IhaveaUIViewwhichcontainsaUITableView.ThetableviewsdelegateissettomyUIView,butitn

I have a UIView which contains a UITableView. The tableview's delegate is set to my UIView, but it never calls the delegate methods:

我有一个包含UITableView的UIView。 tableview的委托设置为我的UIView,但它从不调用委托方法:

-(id)init {
    self = [super init];
    if (self) {
        self.tableView = [[UITableView alloc] initWithFrame:self.bounds];
        self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
        self.tableView.scrollEnabled = NO;
        self.tableView.layer.cornerRadius = PanelCornerRadius;
        [self addSubview:self.tableView];
    }
    return self;
}

#pragma mark - UITableViewDelegate methods

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSLog(@"height for row");
    int height = [self heightForRowAtIndexPath:indexPath];

    return height;
}

#pragma mark - UITableViewDataSource methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    NSLog(@"number of rows");
    if (self.manager.fetchOperation.dataFetchProblem) {
        return 1;
    }

    int numberOfRows = [self.delegate numberOfSectionsInPanel:self];

    return numberOfRows;
}

I've explored every option I can think of, but can't seem to find the root of the problem.

我已经探索了我能想到的每一个选项,但似乎无法找到问题的根源。

EDIT: Included the numberOfRowsInSection. It could potentially return 0, only it never gets that chance because the "number of rows" NSLog never gets called.

编辑:包括numberOfRowsInSection。它可能会返回0,只有它永远不会有机会,因为“行数”NSLog永远不会被调用。

7 个解决方案

#1


16  

Can you please write the code that you have written on your cellForRowAtIndexPath: method? Because i can't find anything wrong with your code.

你能写一下你在cellForRowAtIndexPath:方法上写的代码吗?因为我发现你的代码没有任何问题。

Are you calling your UIView from some other ViewController? If yes, how?

你是从其他ViewController调用你的UIView吗?如果有,怎么样?

I have done something like this once. I declared a method in the UIView like this :

我曾经做过这样的事情。我在UIView中声明了一个方法,如下所示:

- (void)setUpTableView{
    self.tableview.delegate=self;
    self.tableview.dataSource=self;
}

And then i called the setUpTableView from the view controller i added this view to, like this :

然后我从视图控制器调用setUpTableView我添加了这个视图,如下所示:

[self.yourView setUpTableView]; 

This might help. Thanks.

这可能有所帮助。谢谢。

#2


11  

I had a similar problem, my solution was because I did not set the number of sections to 1.

我有类似的问题,我的解决方案是因为我没有将部分数量设置为1。

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

#3


3  

You should declare your view like this @interface MyView : UIView

您应该声明您的视图@interface MyView:UIView

BTW: I would have a ViewController that "controls" your view and your tableview, and have the ViewController be the delegate and datasource of the table view.

顺便说一句:我会有一个ViewController“控制”你的视图和你的tableview,并让ViewController成为表视图的委托和数据源。

#4


3  

You do not need to use a UIViewController, that's just a smokescreen. You also do not need to add to the .h, although you should do that anyway because Xcode will complain otherwise, and you won't get method name auto complete.

你不需要使用UIViewController,这只是一个烟幕。你也不需要添加到.h,尽管你应该这样做,因为Xcode会抱怨,否则你不会自动完成方法名称。

I just wrote a test project that embeds a table view in a normal UIView and it works fine. Just make sure your initialization code is being called. I bet you need to move it to awakeFromNib.

我刚刚编写了一个测试项目,在普通的UIView中嵌入了一个表视图,它运行正常。只需确保调用您的初始化代码。我打赌你需要将它移动到awakeFromNib。

#5


1  

I ran into the same issue once, what silly mistake I did was inside initWithCoder I called [super init]. TableView was in xib

我曾经遇到过同样的问题,我在initWithCoder里面犯了什么愚蠢的错误,我调用了[super init]。 TableView在xib中

-(id) initWithCoder:(NSCoder *)aDecoder
{
    self = [super init]
}

Instead of

代替

-(id) initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
}

Just check if that's not the case

只是检查是否不是这种情况

#6


0  

Try changing your UIViewto UIViewController and in viewDidLoad, call

尝试更改您的UIViewto UIViewController并在viewDidLoad中调用

[[UITableView alloc] initWithFrame:style:] to create your Table View.

[[UITableView alloc] initWithFrame:style:]创建表视图。

Set self as the delegate and data source like you did above, and then add this UITableView as Subview in this Controller.

像上面一样将self设置为委托和数据源,然后在此Controller中将此UITableView添加为Subview。

#7


0  

try this:

尝试这个:

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

i had this stupid problem too

我也有这个愚蠢的问题


推荐阅读
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 本文介绍了UVALive6575题目Odd and Even Zeroes的解法,使用了数位dp和找规律的方法。阶乘的定义和性质被介绍,并给出了一些例子。其中,部分阶乘的尾零个数为奇数,部分为偶数。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 成功安装Sabayon Linux在thinkpad X60上的经验分享
    本文分享了作者在国庆期间在thinkpad X60上成功安装Sabayon Linux的经验。通过修改CHOST和执行emerge命令,作者顺利完成了安装过程。Sabayon Linux是一个基于Gentoo Linux的发行版,可以将电脑快速转变为一个功能强大的系统。除了作为一个live DVD使用外,Sabayon Linux还可以被安装在硬盘上,方便用户使用。 ... [详细]
  • iOS超签签名服务器搭建及其优劣势
    本文介绍了搭建iOS超签签名服务器的原因和优势,包括不掉签、用户可以直接安装不需要信任、体验好等。同时也提到了超签的劣势,即一个证书只能安装100个,成本较高。文章还详细介绍了超签的实现原理,包括用户请求服务器安装mobileconfig文件、服务器调用苹果接口添加udid等步骤。最后,还提到了生成mobileconfig文件和导出AppleWorldwideDeveloperRelationsCertificationAuthority证书的方法。 ... [详细]
  • 本文介绍了如何使用vue-awesome-swiper组件,包括在main.js中引入和使用swiper和swiperSlide组件,以及设置options和ref属性。同时还介绍了如何在模板中使用swiper和swiperSlide组件,并展示了如何通过循环渲染swipes数组中的数据,并使用picUrl属性显示图片。最后还介绍了如何添加分页器。 ... [详细]
  • 使用圣杯布局模式实现网站首页的内容布局
    本文介绍了使用圣杯布局模式实现网站首页的内容布局的方法,包括HTML部分代码和实例。同时还提供了公司新闻、最新产品、关于我们、联系我们等页面的布局示例。商品展示区包括了车里子和农家生态土鸡蛋等产品的价格信息。 ... [详细]
  • 本文详细介绍了Android中的坐标系以及与View相关的方法。首先介绍了Android坐标系和视图坐标系的概念,并通过图示进行了解释。接着提到了View的大小可以超过手机屏幕,并且只有在手机屏幕内才能看到。最后,作者表示将在后续文章中继续探讨与View相关的内容。 ... [详细]
  • Iwanttointegratesort,order,maxandoffsetinafindAllquery.Thefollowingworksfine:我想在fin ... [详细]
  • [转载]从零开始学习OpenGL ES之四 – 光效
    继续我们的iPhoneOpenGLES之旅,我们将讨论光效。目前,我们没有加入任何光效。幸运的是,OpenGL在没有设置光效的情况下仍然可 ... [详细]
  • 涉及的知识点-ViewGroup的测量与布局-View的测量与布局-滑动冲突的处理-VelocityTracker滑动速率跟踪-Scroller实现弹性滑动-屏幕宽高的获取等实现步 ... [详细]
  • Apple iPad:过渡设备还是平板电脑?
    I’vebeenagonizingoverwhethertopostaniPadarticle.Applecertainlydon’tneedmorepublicityandthe ... [详细]
  • Myappcrashedandthecodeisthefollowing:我的应用程序崩溃,代码如下:elseif(){CGDetailVie ... [详细]
  • html结构 ... [详细]
  • java.lang.Class.getDeclaredMethod()方法java.lang.Class.getDeclaredMethod()方法用法实例教程-方法返回一个Met ... [详细]
author-avatar
无与伦比的美丽MJ
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有