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嵌套技 阅读详情

相关推荐

C#集成Bartender:动态图片标签打印的实战与优化

本文详细介绍了在C#程序中集成Bartender实现动态图片标签打印的实战方法与优化策略。通过具名数据源绑定、图片对象尺寸控制等核心技巧,解决了动态图片显示/隐藏的难题,并提供了从模板设计、代码架构到性能优化、错误处理的全流程指南,帮助开发者构建稳定高效的工业级标签打印系统。

weixin_29216957的博客 431

iOS UITableView reloadData问题总结

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

sinat_32425447的博客 7838

爬取中国空气质量在线监测分析平台

1.准备,爬取的链接地址 https://www.aqistudy.cn/html/city_detail.html 2.分析 a.当打开链接后,数据已经设置好了,说明里面大部分都是js通过ajax调用的, b.打开f12分析代码 通过捕获到的数据包,发现当前请求为ajax,携带着一个为d的参数且该请求携带的数据是加密过的,并且响应也是加密过的 c.这时候的问题就是...

Mr Right 6369

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

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

joker4o的博客 3119

海康工业相机BayerRG8转BGR8实战:手把手教你用OpenCV实现高效图像转换

本文详细解析了海康工业相机BayerRG8格式到BGR8格式的高效转换实战。针对工业视觉项目中高帧率、大分辨率场景下的性能瓶颈,文章对比了海康SDK原生方案与OpenCV多种转换方法的优劣,并提供了从基础实现到高级优化(如多线程流水线、内存管理、SIMD指令集应用)的完整代码示例与性能调优策略,帮助开发者构建稳定、高效的图像处理系统。

weixin_29237635的博客 523

PID控制器(位置式和增量式)

PID控制算法

zhinian_hao的博客 4512

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

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

jeffasd的专栏 1万+

TGRS 2026 即插即用 | 注意力篇 | SCFM:双向交叉显著性引导注意力,高层语义与低层细节的双向互补!

本文介绍了MPCNet中的语义引导交叉注意力融合模块(SCFM),该模块通过双向交叉显著性引导机制实现跨尺度特征融合。SCFM创新性地结合了高层语义和低层细节的互补信息,通过轻量局部空间注意力预增强和坐标注意力全局融合引导,显著提升了红外小目标检测的精度。实验表明,SCFM在多个数据集上达到SOTA性能,同时保持轻量高效的计算特性,支持端侧设备部署。 SCFM的核心贡献包括: 双向交叉显著性引导机制,实现语义与细节的互补融合 轻量局部空间注意力预增强模块,计算量仅为传统方法的1/10 坐标注意力全局融合引导

主要更新底层视觉(去噪、超分等)相关的科研内容,形式为【论文精读】+【论文复现】 145

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

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

liuyong0214的博客 2345

为YOLOv11引入加权双向特征金字塔(BiFPN)

本文介绍了将BiFPN应用于YOLOv11模型以提升边缘设备上小目标检测精度的实践。针对传统FPN在特征融合时的信息丢失问题,采用带可学习权重的BiFPN结构进行改造,实现了不同层级特征的动态融合。重点阐述了WeightedFeatureFusion模块的实现细节、neck结构改造方法及训练部署注意事项。实验表明,该改进使小目标AP提升3.1%,参数量仅增加5%,在精度和效率间取得良好平衡。文章还分享了权重初始化、归一化处理等实用技巧,并强调需根据数据特性评估改进效果,重新计算anchor等关键点。

as1225120794的博客 349

SAP UI5 百科全书的目录

笔者 12 年 SAP UI5 开发岁月中写下的 595 篇 SAP UI5 学习笔记,全部收录到《SAP UI5 百科全书》专栏中去,目录如下。请大家按照自己的学习需要,根据关键字搜索到对应的文章,然后进行学习。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 3826

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

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

东风不来的专栏 2147

156、手机摄像头模组结构拆解:从保护盖到 FPC 连接器的完整装配剖面

本文深入解析手机摄像头模组结构设计要点。作者结合项目经验,指出保护盖密封胶在低温收缩会导致边缘漏光问题,强调结构细节比算法参数更关键。文章逐层拆解模组组件:保护盖厚度需平衡抗冲击与光学性能;镜筒内镜片组装配顺序和公差控制直接影响成像质量;IR滤光片厚度公差±0.02mm会改变红外截止波长;CSP封装需注意金线加固;FPC连接器的信号完整性和机械可靠性设计。最后给出5条实用建议,包括密封胶选型、镜片对位检测等,强调模组结构设计是"一票否决制"的基础工程。(149字)

sundong_81的博客 104

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

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

yanglei3kyou的专栏 1万+

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

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

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

georgehenrywilliam的博客 996

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

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

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

FantasyWind-奇风的悟道之地 3158

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、付费专栏及课程。

余额充值