通过xib加载UITableViewCell的新方式
2014-12-03 11:43
405 查看
转自http://blog.csdn.net/yohunl/article/details/19619167
我们以前通常会这样做
- (UITableViewCell
*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString
*CellIdentiferId = @"MomentsViewControllerCellID";
MomentsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentiferId];
if (cell == nil)
{
NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"MomentsCell" owner:nil options:nil];
cell = [nibs lastObject];
cell.backgroundColor = [UIColor clearColor];
};
}
return cell;
}
严重注意:我们之前这么用都没注意过重用的问题,这样写,如果在xib页面没有设置 重用字符串的话,是不能够被重用的,也就是每次都会重新创建,这是严重浪费内存的,所以,需要修改啊,一种修改方式是使用如下ios5提供的新方式:
- (void)registerNib:(UINib *)nib
forCellReuseIdentifier:(NSString *)identifier
还有就是在xib页面设置好(ios5之前也可以用的)
如果忘了在xib中设置,还有一种方式 http://stackoverflow.com/questions/413993/loading-a-reusable-uitableviewcell-from-a-nib
}
还有更常用的
}
但是现在有一种新的方式了,可以采用如下的方式
采用registerNib的方式,并且把设置都放在了willDisplayCell方法中了,而不是以前我们经常用的cellForRowAtIndexPath
这个方法我试了下,如果我们在xib中设置了 Identifier,那么此处的必须一致,否则会crash的
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCustomCell * cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
if (!cell)
{
[tableView registerNib:[UINib nibWithNibName:@"MyCustomCell" bundle:nil]
forCellReuseIdentifier:@"myCell"];
cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
}
return cell;
}
- (void)tableView:(UITableView *)tableView
willDisplayCell:(MyCustomCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.leftLabel.text = [self.items objectAtIndex:indexPath.row];
cell.rightLabel.text = [self.items objectAtIndex:indexPath.row];
cell.middleLabel.text = [self.items objectAtIndex:indexPath.row];
}
我们以前通常会这样做
- (UITableViewCell
*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString
*CellIdentiferId = @"MomentsViewControllerCellID";
MomentsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentiferId];
if (cell == nil)
{
NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"MomentsCell" owner:nil options:nil];
cell = [nibs lastObject];
cell.backgroundColor = [UIColor clearColor];
};
}
return cell;
}
严重注意:我们之前这么用都没注意过重用的问题,这样写,如果在xib页面没有设置 重用字符串的话,是不能够被重用的,也就是每次都会重新创建,这是严重浪费内存的,所以,需要修改啊,一种修改方式是使用如下ios5提供的新方式:
- (void)registerNib:(UINib *)nib
forCellReuseIdentifier:(NSString *)identifier
还有就是在xib页面设置好(ios5之前也可以用的)
如果忘了在xib中设置,还有一种方式 http://stackoverflow.com/questions/413993/loading-a-reusable-uitableviewcell-from-a-nib
NSArray * nibObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomTableCell" owner:nil options:nil]; for (id obj in nibObjects) { if ([obj isKindOfClass:[CustomTableCell class]]) { cell = obj; [cell setValue:cellId forKey:@"reuseIdentifier"]; break; }
}
还有更常用的
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return[self.items count];} -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:@"myCell"]; if(!cell){ cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"];} cell.textLabel.text =[self.items objectAtIndex:indexPath.row]; return cell;
}
但是现在有一种新的方式了,可以采用如下的方式
采用registerNib的方式,并且把设置都放在了willDisplayCell方法中了,而不是以前我们经常用的cellForRowAtIndexPath
这个方法我试了下,如果我们在xib中设置了 Identifier,那么此处的必须一致,否则会crash的
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCustomCell * cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
if (!cell)
{
[tableView registerNib:[UINib nibWithNibName:@"MyCustomCell" bundle:nil]
forCellReuseIdentifier:@"myCell"];
cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
}
return cell;
}
- (void)tableView:(UITableView *)tableView
willDisplayCell:(MyCustomCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.leftLabel.text = [self.items objectAtIndex:indexPath.row];
cell.rightLabel.text = [self.items objectAtIndex:indexPath.row];
cell.middleLabel.text = [self.items objectAtIndex:indexPath.row];
}
相关文章推荐
- 通过xib加载UITableViewCell的新方式
- 通过xib加载UITableViewCell的新方式
- 通过xib加载UITableViewCell的新方式
- 通过xib加载UITableViewCell的新方式
- 从Xib文件加载UIView的5种方式
- 让uBoot通过tftp的方式加载Linux内核文件 .
- 让uBoot通过tftp的方式加载Linux内核文件
- Cocos2d-x之CCSpriteFrameCache 通过帧的方式加载一张大图
- 通过Nib/Xib来加载UIView的注意事项
- Android 应用开发 之通过AsyncTask与ThreadPool(线程池)两种方式异步加载大量数据的分析与对比
- 通过FreeMarkerclasspath加载方式生成静态页面
- 批量修改Texture属性 两种方式加载贴图,通过Resources.LoadAll Directoy 获取项目路径:Application.dataPath; using UnityEn
- SpringMVC实现velocity多Resourceloader通过文件、数据库等方式加载模版
- 如何通过修改快捷方式目标属性加载插件
- 自定义UITableViewCell (通过XIB:每个CELL形状一样适合用XIB来创建CELL)
- ios – 使用UINib加载xib文件实现UITableViewCell
- 通过storyboard和xib创建控制器的方式
- 黑马程序员----加载配置文件和通过内省方式操作javabean总结
- 让uBoot通过tftp的方式加载Linux内核文件
- 让uBoot通过tftp的方式加载Linux内核文件