dequeueReusableCellWithIdentifier方法问题
2012-08-24 15:34
302 查看
static NSString *CellTableIdentifier = @"CellTableIdentifier ";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
CellTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellTableIdentifier] autorelease];
}
tableView实现是这样的,它并不创建所有行,比如你的表格数据有100行,但是屏幕上的空间只够显示10行,那么tableView只会创建10个左右的cell,当你滚动时,有些行会被遮住,这些被遮住的行就会被回收放入它的回收空间,而将要出现的行会首先在回收空间查找是否有空闲的cell,如果找到就使用,这样避免了创建cell带来的开销,节省空间和时间。这时的cell里的内容是旧的,你必需更新它的内容为将要出现的行的内容。
更新时注意点,如果你自己添加了视图到cell,你必需首先移除这个视图
UIView *view = [cell viewWithTag:1];//1为你自己的添加的视图的tag属性的值,view.tag = 1;
if (view)
{
[view removeFromSuperView];
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
CellTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellTableIdentifier] autorelease];
}
tableView实现是这样的,它并不创建所有行,比如你的表格数据有100行,但是屏幕上的空间只够显示10行,那么tableView只会创建10个左右的cell,当你滚动时,有些行会被遮住,这些被遮住的行就会被回收放入它的回收空间,而将要出现的行会首先在回收空间查找是否有空闲的cell,如果找到就使用,这样避免了创建cell带来的开销,节省空间和时间。这时的cell里的内容是旧的,你必需更新它的内容为将要出现的行的内容。
更新时注意点,如果你自己添加了视图到cell,你必需首先移除这个视图
UIView *view = [cell viewWithTag:1];//1为你自己的添加的视图的tag属性的值,view.tag = 1;
if (view)
{
[view removeFromSuperView];
}
相关文章推荐
- (0069)iOS开发之dequeueReusableCellWithIdentifier的两个方法的区别
- iOS-UITableView的两种重用Cell方法的区别(dequeueReusableCellWithIdentifier)
- iOS-UITableView的两种重用Cell方法的区别(dequeueReusableCellWithIdentifier)
- UITableView中的dequeueReusableCellWithIdentifier的方法
- UITableView学习之辨析两个方法:⓵dequeueReusableCellWithIdentifier与⓶dequeueReusableCellWithIdentifier:forIndexPath:
- [ios专项]几个遗留问题dequeueReusableCellWithIdentifier_2014-05-29
- UITableView 关于dequeueReusableCellWithIdentifier的问题
- dequeueReusableCellWithIdentifier:和dequeueReusableCellWithIdentifier: forIndexPath:的问题
- Nhibernate出现No row with the given identifier exists问题的产生原因及解决方法
- 关于TableViewCell生成时dequeueReusableCellWithIdentifier的认识
- dequeueResableCellWithIdentifier方法
- 关于TableViewCell生成时dequeueReusableCellWithIdentifier的认识
- 关于TableViewCell生成时dequeueReusableCellWithIdentifier的认识
- iOS 运行出现:Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /Sourc
- dequeueReusableCellWithIdentifier
- 区别dequeueReusableCellWithIdentifier 和dequeueReusableCellWithIdentifier: forIndexPath
- Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:]
- dequeueReusableCellWithIdentifier 时要慎重
- UITableView中的dequeueReusableCellWithIdentifier使用
- 关于TableViewCell生成时dequeueReusableCellWithIdentifier的认识