UITableView的reloadData方法,不执行cellForRow方法,

[求助]iOS调用reloadData()不刷新数据,不调用cellForRowAt方法 代码如下:主线程调用了tableView的reloadData()之后只调用了numberOfRowsInSection,没有调用cellForRowAt一直找不到解决的方法还有出错的原因,希望有大神知道原因的能指点一下,感激不尽import UIKitprotocol SearchViewDelegate { ///点击排序按钮调用 func searchView(clickSor 阅读详情

首先是只有tableview将要显示在window上的时候才会进入cellForRow方法。

像self.tableView.delegate = self;啊,dataSource数组为空啊,相信都检查过了。

也许你会说,我的tableview添加了啊,但是显示一片空白,这时候你给tableview设置个背景色看看,也许你就会发现,tableview并没有显示出来。

原因可能有多种,譬如我的原因是:

ios7以前的版本,subviews不随superview的变化而变化,也就是view的clip属性可能设置错了。

如果你设置了tableview的size为(100,100),但是superview的size是(0.0),这时仍然算作tableview没有显示到window,所以不会调用cellForRow方法。

还有很多中导致tableview没有显示到window的可能

对了在创建视图的时候记住:绝对不在init方法中创建,修改视图,哪怕是get方法加载视图

更新

检查顺序

1-代理是否设置

2-tableView的frame是否为0

3-如果使用InterfaceBuilder检查代理 IBOutlet

4-是否在主线程中reloadData  

  • (void) tocLoad { dispatch_async(dispatch_get_main_queue(), ^{ [self.tableView reloadData]; }); }

最后附上stackoverFlow

When I call [tableView reloadData] the function - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath is not fired? Why?

Here's my TableView.m

@implementation TableView
@synthesize managedObjectContext;
@synthesize controller = _controller;
@synthesize tableView = _tableView;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

    }
    return self;
}
-(id)init
{
    self = [super init];
    if (self) {
        NSLog(@"%s",__PRETTY_FUNCTION__);
        self.del = [[UIApplication sharedApplication] delegate];
        self.managedObjectContext = self.del.managedObjectContext;

        [self performControllerFetch];
    }
    return self;
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    ...
}

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

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

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    ...
}

-(void)reloadTableView
{
    NSLog(@"%s",__PRETTY_FUNCTION__);

    [self.tableView reloadData];

}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {

    NSLog(@"%s",__PRETTY_FUNCTION__);
    UITableView *tableView = self.tableView;

    switch(type) {

        case NSFetchedResultsChangeInsert:
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeUpdate:
            [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                                  withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeMove:
            [tableView deleteRowsAtIndexPaths:[NSArray
                                               arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [tableView insertRowsAtIndexPaths:[NSArray
                                               arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}


- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id )sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {

    switch(type) {

        case NSFetchedResultsChangeInsert:
            [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}


- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    // The fetch controller has sent all current change notifications, so tell the table view to process all updates.
    [self.tableView endUpdates];
}

@end

TableView.h

@interface TableView : UITableView <UITableViewDataSource, UITableViewDelegate, NSFetchedResultsControllerDelegate>

@property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, strong) AppDelegate *del;
@property (nonatomic, strong) NSFetchedResultsController *controller;
@property (weak, nonatomic) IBOutlet TableView *tableView;

-(void)reloadTableView;
-(void)performControllerFetch;
@end
share improve this question
 
1 
Why downvoting? –  iCode  Oct 10 '13 at 17:45
2 
Downvote seems a little misplaced, this is a legitimate question with what looks like some important design considerations that the asker should be aware of. –  Alfie Hanssen  Oct 10 '13 at 17:51

4 Answers

up vote 8 down vote accepted

It looks like you are never setting the delegate and datasource properties on your tableview, no? You're implementing the methods in those protocols inside tableview.m but not setting the two properties to self hence calling reloadData having no effect.

Does this help?

Edit:

Looks like you have a tableView property set on a subclass of UITableView and hooked up to an interface builder file. This is an odd setup (a tableview within a tableview). Usually you would have something like a UIViewController subclass hooked up to an XIB and set an

@property (nonatomic, strong) IBOutlet UITableView * tableView

in that subclass, or something similar. And then handle the data fetching and tableview delegate/datasource inside that viewcontroller subclass.

But here, because of the nested UITableViews it's not as straightforward. Is there a reason for this design? I recommend moving to something like I describe above to help bring clarity to the setup.

Also, try adding some NSLog statements into your init method to see if the tableview != nil and the delegate and datasource properties have been set. My suspicion is that the connection is not being made.

Also, unrelated to the problem at hand, you no longer have to manually @synthesize ivar = _ivar, it will be done for you automatically using the underscore convention.

share improve this answer
 
 
I've set the delegate and the datasource in the Interface Builder to the class TableView.m. Does that reach? –  iCode  Oct 10 '13 at 17:31
 
Hmm, Can you post the .h file as well (what class does tablview.m inherit from)? And can you post the code where you declare your IBOutlets (if any)? Sometimes IB can be picky about whether you make the delegate/datasource connection with the "files owner" vs the view within the XIB file. –  Alfie Hanssen  Oct 10 '13 at 17:35
 
I've updated my code. btw: the data in the table is displayed correctly. –  iCode  Oct 10 '13 at 17:38 
 
Thanks now it is working (I've done what you've suggested)! –  iCode  Oct 10 '13 at 18:40 
 
Aha, that's great news! –  Alfie Hanssen  Oct 10 '13 at 19:00

Inspite of writing this [self.tableView reloadData]; try below, becuase already you have written class so no need to take the outlet again:-

[self reloadData]
share improve this answer
 
 
Still not working. –  iCode  Oct 10 '13 at 17:43
 
Basically what is your problem i mean it is not refreshing the data?? –  Hussain Shabbir  Oct 10 '13 at 17:45
 
Yes that's the problem. –  iCode  Oct 10 '13 at 17:46
 
Ok do one thing disconnect you outlet,delegate and datasource of tableview and then only reconnect delegate and datasource of your table to your controller class and check –  Hussain Shabbir  Oct 10 '13 at 17:52
 
Basically that method should call when you connect delegate to the tableview –  Hussain Shabbir  Oct 10 '13 at 17:53

I was having the same issue, that the reload was not working. I believe the delegate method I was calling it from was in a background thread which was causing the problem. My solution was to create a new method (below) and call the reload from there instead, while also ensuring to call it from the main thread:

  • (void) tocLoad { dispatch_async(dispatch_get_main_queue(), ^{ [self.tableView reloadData]; }); }
share improve this answer
 

I spent days debugging this issue, and found a ton of possible fixes. I starred the one that worked for me but they're all valid.

First, of course, check that your delegate, datasource, IBOutlet, etc are all configured properly.

**Set the All Exceptions breakpoint and see if you are getting an out of bounds exception in your datasource. Without setting the breakpoint there is no crash and the tableview will simply fail to reload.

Other possible fixes: check the tableview's frame to make sure the width or height is not 0. Try executing reloadData on the main thread. Lastly, try only reloading a single section (here).

Gotta love table views.


评论楼中楼:UITableView嵌套UITableView实现方案与避坑 UITableView是iOS开发中高频使用的列表组件,其核心机制在于通过数据源驱动高效渲染结构化行数据。当业务需要呈现“评论的评论”这类二级关联数据时,单层列表难以直观表达评论与回复的父子关系,这时嵌套UITableView便成为常见解法:在外层评论Cell中嵌入内层列表,使回复自然挂在对应评论下。该方案涉及动态行高计算、复用状态清理、滚动事件归一等关键问题,处理好这些细节能明显提升复杂评论区的稳定性与交互流畅度,并直接关系到社交App、电商评价、资讯文章等场景的用户体验。掌握UITableView嵌套技 阅读详情

相关推荐

Matlab激活失败?手把手教你解决License checkout failed问题(附详细截图)

本文针对Matlab激活后出现“License checkout failed”错误的常见问题,提供了详细的解决方案。核心在于精准定位并正确填写Windows系统用户名,而非计算机名或账户全名。文章通过分步截图和命令指引,手把手教你使用`whoami`命令获取正确信息,并重新运行激活程序,从根本上解决因用户名填写错误导致的许可检查失败问题。

weixin_29062823的博客 570

iOS UITableView reloadData问题总结

iOS UITableView reloadData 方法 无效,或者不执行,导致 TableView 不刷新 原因。 转载 http://www.cnblogs.com/zxykit/p/5664192.html 人家总结的很到位 reloadData不执行有好多总情况 1.没有实现代理,这是最最基本的 _tableView.dataSource=self; _tableV

sinat_32425447的博客 7838

平面变压器PCB绕组设计实战:从原理到布局的工程考量

本文深入探讨了平面变压器PCB绕组设计的工程实践,从基础原理到布局优化,涵盖了电流密度、层间绝缘、绕组拓扑结构等关键参数。通过实战案例分享,解析了高频应用中的集肤效应、邻近效应等挑战,并提供了热管理和EMC优化的实用技巧,助力工程师在服务器电源、车载充电器等空间敏感场景实现高效设计。

weixin_42666036的博客 452

collectionview reloaddata 刷新失效或者 不走cellforrow刷新cell

dispatch_async(dispatch_get_main_queue(), ^ { //加入到主线程,强制执行reload [self.collectionView reloadData]; }) [self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];//指定刷新的section

joker4o的博客 3119

【电子元件篇】电容的九大作用与选型指南-超简单解读

本文系统介绍了电容的基础概念、核心功能及实际应用。电容作为存储电荷的被动元件,其容值公式为C=Q/V,单位包括法拉及常用派生单位。文章重点解析了电容的九大功能:滤波(通过RC低通电路抑制噪声)、耦合(传递交流隔离直流)、去耦(稳定供电)、储能(短暂供电)、定时(RC延时)、谐振(RLC振荡)、旁路(高频通路)、电压调节(DC-DC转换)以及保护(吸收浪涌)。同时详细阐述了电容分类(陶瓷/电解/薄膜等)、参数选型要点(耐压/容值/精度/温度)及PCB设计注意事项,并针对不同应用场景提供了具体选型建议和常见误区

2301_81299416的博客 4006

利用python求解规划问题

利用python中的cvxpy,sympy,scipy这三个库进行规划问题求解,本文中的例题均来自司守奎老师的python数学建模算法与应用

dashbaani的博客 1万+

tableView调用reloadData不刷新, cellForRowAtIndexPath方法不走

http://blog.sina.com.cn/s/blog_6451df3d0102vrrz.html 记录一下蛋疼的bug. 我用AFNetworking 做网络请求,回调成功后 执行[self.tableView reloadData] 二次刷新界面;  但是numberOfRows走,cellForRowAtIndexPath 方法却不走了.

jeffasd的专栏 1万+

SDM660电源部分配置:

SDM660电源部分配置: Panel power supplies 1、Reference power supplies Description Name Regulator Voltage (V) Panel power supply 0 vddio PMIC 660/L11 1.8 Panel power supply 1 Lab PMIC 660/lab 4.6 Panel power s...

weixin_41811215的博客 4728

iOS UICollectionView— reloadData后变空白,cellForItemAtIndexPath不执行

最近项目在使用UICollectionView的时候,发现一个大坑,问题描述: 1、无法全部加载数据,contentSize是正确的,可以滑动 2、下拉刷新的时候,执行了reloadData,页面出现空白,并且不执行delegate 解决思路: collectionView执行了reloadData代码后,没有执行cellForItem代理方法,可能存在两种原因: 1、在执行reloadData后,可能collectionView被一层假的view所遮挡,这种情况下是不会执行cellForItem

liuyong0214的博客 2345

YOLO26全新改进 | 最全损失函数改进篇 | SIoU 、WIoU、GIoU、DIoU、EIOU、CIoU, InnerIoU、InnerSIoU、InnerWIoU、FocusIoU等损失函数

🔥本文详细介绍了如何修改YOLO26中的损失函数。首先提供了完整的损失函数核心代码,包括WIoU_Scale类和各种IoU计算方法。然后分三步指导修改:1)替换metrics.py中的bbox_iou方法;2)修改loss.py中的iou计算代码;3)更新tal.py中的相关代码。文中特别强调了修改损失函数应在模型结构调整后使用,并提供了多种IoU损失函数的参数配置建议,包括SIoU、WIoU、GIoU等,可根据实际需求灵活选择搭配使用。

Ai缝合怪、助力小伙伴高效发论文 445

2026年CSP-J初赛真题及答案解析

2026年CSP-J初赛真题及题解

王老师青少年编程 1615

UITableView的reloadData方法,不执行cellForRow方法,但是执行了numberOfRows和heightForRow方法

可能是因为cell上有图片的异步加载,只需将reload放在主线程就行了:dispatch_async(dispatch_get_main_queue(), ^{ [self.tableView reloadData]; });

东风不来的专栏 2147

【学习系列】SAP RAP 22:Excel文件上传和读取(非扩展&扩展方式)

本文介绍了在RAP中实现Excel文件上传处理的三种方法:1) 使用@Semantics.largeObject注解实现简单上传下载,但限制在ObjectPage页面使用;2) 通过自定义action弹窗实现上传功能,但存在无法自动刷新列表等限制;3) 结合CustomUIExtension实现更灵活的完整功能,但需要UI5开发技能。文章详细说明了每种方法的实现步骤,包括数据库表设计、注解配置、前端界面开发和后端处理逻辑,并比较了各自的优缺点。最后通过一个处理不同币种数据的示例,展示了如何实现业务处理逻辑。

DeveloperMrMeng的博客 621

iOS 探讨之 cellForRow/cellForItemAtIndexPath 函数不执行

阐述 日常开发中总能遇见一些奇葩的状况,事后发现都是因一些“小忘记”而产生,不过有一些是挺奇葩的。 估计大家刚看标题 "cellForRow/cellForItemAtIndexPath函数不执行" 第一反应就是数据源(dataSource)未设置或者是数据源中的数据总量为零。 大家的反应都是合理的,但我今天想提出的情景是当视图尺寸(Size)宽(width)或高(height)为零的时候表

yanglei3kyou的专栏 1万+

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

iOS中UITableView刷新数据reloadDate之后不立即执行cellForRow的方法 导致多个数据源对应一个tableview数据无法显示

这几天在做项目的时候,需要在刷新数据后,cell显示完成后,发送通知: [self.tableView reloadData]; 添加通知: [self addNotifi]; 本来的想法的是:刷新数据后执行cellForRow的方法,然后再执行通知里面的方法: 执行顺序为: reloadData ------>cellForRow的方法 -...

georgehenrywilliam的博客 996

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

UITableView reloadData后,cellForRowAtIndex方法未调用问题

问题描述tableView刷新后,cellForRowAtIndex方法并未被调用,所以cell内容实际未刷新。问题解决搞了一个小时,终于查到一个相同的问题: tableView调用reloadData不刷新, cellForRowAtIndexPath方法不走问题原因出现这个问题的原因应该是UITableView初始化时,frame的height太小,即便之后在其他方法里改回来,也会导致这个问题

FantasyWind-奇风的悟道之地 3158

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

UITaUITableView 不进行reloadData改变Cell的高度

2019独角兽企业重金招聘Python工程师标准>>> ...

weixin_34217773的博客 739

【iOS 开发】tableView updates 对比 reloadData

Paste_Image.png 如图有一个 TableView,每行显示这一行是第几行,现在我希望每按一次 update 按钮,就动态地在下方加两行。那么简单粗暴的做法是 ,更改数据源,然后刷新一下列表: // tableData = ["0", "1", "2", "3"] @IBActio...

weixin_33782386的博客 234

af网络请求里,刷新tableview,reloaddate方法执行两次

其实并不是执行两次,之所以在cellforheight跟cellforrow方法里打印了两遍的结果是因为,             一:使用了xib来展示tableview。二:reloaddata不会等待tableview完全刷新完才返回,而是立即返回,这也就是输,你加在reloaddata后边的代码会立即执行(包括对某个,某些cell的操作),然后tableview可能才执行,这样引起的后果就...

a787188834的专栏 1077

记录一个tableView刷新不及时导致的数组越界崩溃

1 崩溃报错信息 [__NSArrayI objectAtIndexedSubscript:]: index 2 beyond bounds [0 .. 1] 明显的数组越界 这里是崩溃时的调用栈,崩溃在了cellForRow方法中 情景分析: 时机是在我点击tabbar 的 me页面 的时候, me页面的viewWillappear 中会执行一些操作改变数据源,但是改变数据源之后没有reloadData reloadData是在viewDidapear中执行的 但是,由于我们.

liubo的博客 848

如何用ExpandableCell实现iOS表格的优雅展开/折叠效果?

在iOS开发中,实现表格的展开和折叠功能一直是一个常见但繁琐的任务。传统的`insertRows`和`deleteRows`方法不仅代码复杂,还容易出错。今天,我要向大家介绍一个终极解决方案——**ExpandableCell**,这是一个用Swift 5编写的强大iOS库,能够让你轻松实现优雅的表格展开/折叠效果。 ExpandableCell是一个专门为iOS开发者设计的开源库,它完全重构了

gitblog_00774的博客 298

十年iOS开发复盘:从iOS 9到iOS 19,技术演进与工程实践

在移动开发领域,语言与框架的演进始终驱动着技术选型与工程实践。从Objective-C到Swift的过渡,不仅改变了编码方式,更深刻影响了App的性能表现与开发效率。随着系统版本迭代,内存管理、启动优化、崩溃排查成为保障用户体验的关键环节。跨平台方案的涌现与AI能力的集成,又为原生开发带来新的挑战与机遇。本文基于十年iOS开发经验,从iOS 9到iOS 19,梳理UIKit与SwiftUI的实践策略,分享内存管理、性能调优、崩溃治理的实战方法,并探讨跨端技术选型与CoreML应用,为开发者提供可参考的技术路

weixin_34342992的博客 354

iOS tableView reloadData不生效或出现问题

iOS tableView reloadData不生效或出现问题有时在调用tableView的刷新,界面会出现异常或是不生效,在排除了基本的原因,像是delegate或是dataSource的问题后,可能还有这种情况. 比如在table的这个代理方法中- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSInde

ZzzzzzXJ的专栏 5791

tableView reloadData时cellForRowAtIndexPath不调用问题的小结

问题:tableView需要显示的数据内容是网络请求过来的,tableView是另外写在UIView中,不在控制器里面,所以我在UIView里面加了接口,并且在视图控制器里请求到数据后传回给tableView。 调用接口的时候我实例化了tableView的一个类。 结果cellForRowAtIndexPath在刷新的时候不会被调用,表格虽然有,但内容显示为空。 然后我就在拿到数据...

516

iOS tableView不走cellForRowAtIndexPath

可能原因: tableview被设置成了CGRectZero,也就是tableView的width和height都是0

sgliquangang的专栏 354

UICollectionView reloadData不走cellForRowAtIndexPath的问题

这几天在用UICollectionView 的时候遇到一个问题,找一好大一会,参考大神 http://blog.youkuaiyun.com/ihaveonedream/article/details/51446545 的解决方案给解决了,写在这里记录一下,以后再遇见了忘记如何解决时看见此文应能迅速处理。

opurplestar的博客 4192
上一篇: Xcode调试-值为nil的情况分析
下一篇: tableView Crash
kingcodexl
博客等级 码龄12年 18粉丝 49原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值