ios UITableView的cellForRowAtIndexPath方法不执行的原因
来源:互联网 发布:流量软件哪个好 编辑:程序博客网 时间:2024/05/17 22:35
众所周知,UITableView是非常常用的UI,但是有时候我们会碰到UITableViewUITableViewDataSource协议中cellForRowAtIndexPath不执行的情况,原因有可能如下:
1.tableView的宽度或高度等于0;
在这种情况下numberOfSectionsInTableView和numberOfRowsInSection都执行,而cellForRowAtIndexPath不执行。
2.没有设置tableView的dataSource属性;
发生这种情况的原因有可能是:
(1)自己失误忘了写;
(2)mainTableView.delegate = self;写了两次,就像这样:
mainTableView.delegate = self;
mainTableView.delegate = self;
这就纯属笔误了。
在这种情况下UITableViewDataSource协议中的任何方法都不会执行。
3.numberOfSectionsInTableView方法返回值为0;
在这种情况下numberOfSectionsInTableView会执行,numberOfRowsInSection就不会被执行了。
4.numberOfRowsInSection方法返回0;
在这种情况下cellForRowAtIndexPath不会被执行。
部分测试代码:
#import "ViewController.h"@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>{ UITableView *mainTableView;}@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self initUI];}- (void)initUI{ //高度设置为132是因为不实现UITableViewDelegate中的heightForRowAtIndexPath方法,cell默认高度为44,132 = 44 * 3; mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, 375, 132) style:UITableViewStylePlain]; mainTableView.delegate = self; mainTableView.dataSource = self; [self.view addSubview:mainTableView];}#pragma mark -- <UITableViewDelegate,UITableViewDataSource>- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ NSLog(@"numberOfSectionsInTableView"); return 1;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ NSLog(@"numberOfRowsInSection"); return 10;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"cellForRowAtIndexPath"); UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID"]; cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cellID"]; cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row]; return cell;}
阅读全文
0 0
- ios UITableView的cellForRowAtIndexPath方法不执行的原因
- tableview:cellForRowAtIndexPath:不执行的原因
- UITableView代理方法不执行的原因
- UITableView代理cellForRowAtIndexPath不执行的相关解决方案
- UITableView代理cellForRowAtIndexPath不执行的相关解决方案
- UITableView的代理方法viewForHeaderInSection不执行的原因
- UITableView的代理方法viewForHeaderInSection不执行的原因
- 为什么TableView 的代理方法cellForRowAtIndexPath 不执行问题
- UITableView代理方法没有执行的原因
- iOS cellForRowAtIndexPath 不调用原因分析
- swift iOS 不执行deinit的原因
- IOS cellForRowAtIndexPath方法不被调用
- 关于tableView的cellForRowAtIndexPath方法不走的一个解决办法
- UITableView中didSelect方法没有执行的可能原因
- cellForRowAtIndexPath不执行
- cellForRowAtIndexPath不执行
- cellForRowAtIndexPath不执行
- onActivityResult方法不执行的一个原因
- 线程安全和可重入函数
- 好程序员的考核指标
- Android NDK开发Crash错误定位
- HashMap和Hashtable的区别
- Java 处理异常
- ios UITableView的cellForRowAtIndexPath方法不执行的原因
- SVM入门级别简单总结
- 记:combotree中getValue和getText问题
- synchronized的可重入性
- Java实现-链表排序
- Struts2简短的学习笔记
- js假分页(tabel)
- LeetCode 65. Valid Number
- NI/NDK开发指南(开山篇)