创建cell的几种方式
2016-03-28 00:45
381 查看
方式一 注册cell -> 无需为cell绑定标识符 [使用UIViewController完成!]
l 1> static NSString * const ID = @"cell"; // 全局ID变量
l 2> 在视图加载完成后使用tableView进行注册cell
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
}
l 3> 在数据源方法cellForRowAtIndexPath:中直接从缓存池中取cell即可!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 如果是注册cell.那么下面从tableView缓存池中取cell两种的方式方式都可用!
// 获取cell 方式一
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 获取cell 方式二
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@"cell - %zd",indexPath.row];
return cell;
}
方式二 为cell绑定标识符 -> storyboard中进行设置
l 1> 在storyboard中为cell绑定标识符
l 2> 在数据源方法cellForRowAtIndexPath:中直接从缓存池中取cell即可!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// 无需判断,直接获取即可!
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@"cell - %zd",indexPath.row];
return cell;
}
方式三 在数据源方法cellForRowAtIndexPath:中直接设置!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *ID = @"cell";
// 如果是创建局部变量的ID.那么下面这种从缓存池中取cell的方式会使程序崩溃! // 程序崩溃代码! 错误!
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
// 程序正确!
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
cell.textLabel.text = [NSString stringWithFormat:@"cell - %zd",indexPath.row];
return cell;
}
l 1> static NSString * const ID = @"cell"; // 全局ID变量
l 2> 在视图加载完成后使用tableView进行注册cell
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
}
l 3> 在数据源方法cellForRowAtIndexPath:中直接从缓存池中取cell即可!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 如果是注册cell.那么下面从tableView缓存池中取cell两种的方式方式都可用!
// 获取cell 方式一
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 获取cell 方式二
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@"cell - %zd",indexPath.row];
return cell;
}
方式二 为cell绑定标识符 -> storyboard中进行设置
l 1> 在storyboard中为cell绑定标识符
l 2> 在数据源方法cellForRowAtIndexPath:中直接从缓存池中取cell即可!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// 无需判断,直接获取即可!
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@"cell - %zd",indexPath.row];
return cell;
}
方式三 在数据源方法cellForRowAtIndexPath:中直接设置!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *ID = @"cell";
// 如果是创建局部变量的ID.那么下面这种从缓存池中取cell的方式会使程序崩溃! // 程序崩溃代码! 错误!
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
// 程序正确!
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
cell.textLabel.text = [NSString stringWithFormat:@"cell - %zd",indexPath.row];
return cell;
}
相关文章推荐
- UITableviewcell的性能问题
- UITableview控件简单介绍
- IOS单例的实现
- IO五种模式
- ie兼容css-zoom作用
- hdu5652 India and China Origins
- 12.结账流程(Checkout Process)
- iOS蓝牙4.0开发
- hashtable的C++实现
- Thread详解5:synchronized的使用(一)
- 导航控制器属性和基本使用
- 最完整的Java IO流学习总结
- epoll用发
- 多控制器和导航控制器简单介绍
- android Theme使用三
- hdu5651xiaoxin juju needs help(大组合数模板)
- 11-21 Ubuntu下MySQL的安装及远程连接配置
- c++作业2
- 20159313《网络攻击与防范》第四周学习总结
- “五问”找钢网交易模式