之前虽然已经学习IOS开发很久了,但是发现好多东西都没有理解到位。今天才彻底弄明白UITableVIewCell的创建过程,所以详细写下来,以供以后参考理解。
UITableView通过调用方法 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath来生成单元格UITableViewCell,
而创建生成UITableViewCell的最大个数则有每个cell的高度height和窗口高度screenHeight共同决定,也即生成的cell个数为count=screenHeight/height;
当count的个数小于需要填充的数据表dataArray的个数时,每次通过重新填充已生成cell的数据来实现内容更新。
例如:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellString = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellString]; // 1
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: CMainCell] autorelease]; // 2
}
[cell setData:_data];//3
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat height;
return height; //4
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return arrayCount;//5
}
设定整个手机可视屏幕高度为screenHeight,则程序中执行语句2的次数为screenHeight/height,也就是最多生成这么多个cell。
如果arrayCount<=(screenHeight/height),生成的cell个数为arrayCount;如果arrayCount>(screenHeight/height),生成的cell个数为screenHeight/height,此时多出的cell通过语句3动态填充数据实现。