众所周知,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 ()
{
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 --
- (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;
}
荒野乱斗国际版 (BrawlStars)最新版本v56.274
下载一波超人内置修改器菜单版 安卓版v1.0.2
下载敢达决战官方正版 安卓版v6.7.9
下载敢达决战 安卓版v6.7.9
下载继承了一座戏园子无限声望铜钱版 内置菜单最新版v1.7
继承了一座戏园子折相思版是游戏的破解版本,在该版本中为玩家提
山河半世橙光清软金手指版 无限鲜花v3.24
山河半世是一款超级好玩的橙光恋爱游戏,在游戏中玩家们需要扮演
蓬莱手游折相思版 安卓版v1.0.0
蓬莱免广告版是游戏的修改版本,在该版本中为玩家去除了广告,玩
当红影后橙光游戏破解版2025 最新版v1.0
当红影后橙光破解版是一款超级好玩的娱乐圈题材的橙光游戏,在这
忽然成了万人迷清软完结版 无限鲜花版v12.15
忽然成了万人迷破解版是一款非常好玩的男性向橙光游戏,在有一天