UITableView中的dequeueReusableCellWithIdentifier的方法

在使用UITableView控件的时候,datasource的代理方法经常会使用到下面的方法来加载UITableView的数据显示

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *CustomCellIdentifier = @"CustomCellIdentifier";

DiscountProductCell * cell=  (DiscountProductCell*)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];

if (cell
== nil) {

NSArray * nib = [[NSBundle mainBundle] loadNibNamed:@"DiscountProductCell" owner:self options:nil] ;

cell = [nib objectAtIndex:0];

}

cell.item =
mPushItem;

cell.selectionStyle = UITableViewCellSelectionStyleNone;

UIImageView *backgroundView =
[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"list"]] autorelease];

cell.backgroundView =
backgroundView;

UIImageView *accessoryView =
[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ad1"]] autorelease];

[accessoryView setFrame:CGRectMake(0.0f, 0.0f, CATEGORY_CELL_AC_WIDTH, CATEGORY_CELL_AC_HEIGHT)];

cell.accessoryView =
accessoryView;

return cell;

}

代码中 DiscountProductCell *
cell=  (DiscountProductCell*)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];是每个

cellForRowAtIndexPath方法都必须用到的。其中dequeueReusableCellWithIdentifier的意义是什么呢?

tableView实现是这样的,它并不创建所有行,比如你的表格数据有100行,但是屏幕上的空间只够显示10行,那么tableView只会创建10个左右的cell,当你滚动时,有些行会被遮住,这些被遮住的行就会被回收放入它的回收空间,而将要出现的行会首先在回收空间查找是否有空闲的cell,如果找到就使用,这样避免了创建cell带来的开销,节省空间和时间。这时的cell里的内容是旧的,你必需更新它的内容为将要出现的行的内容。

时间: 2024-08-07 20:49:26

UITableView中的dequeueReusableCellWithIdentifier的方法的相关文章

解决UITableView中Cell重用机制导致内容出错的方法总结

UITableView继承自UIScrollview,是苹果为我们封装好的一个基于scroll的控件.上面主要是一个个的 UITableViewCell,可以让UITableViewCell响应一些点击事件,也可以在UITableViewCell中加入 UITextField或者UITextView等子视图,使得可以在cell上进行文字编辑. UITableView中的cell可以有很多,一般会通过重用cell来达到节省内存的目的:通过为每个cell指定一个重用标识符 (reuseIdentif

重写UITableViewCell子类中属性的setter方法来实现隐藏或显示该cell中的某些控件

为什么会需要这样子的一种方法来实现隐藏或者显示一个cell中的某些控件呢? 其实,隐藏cell中某些控件可以直接在tableView:cellForRowAtIndexPath:方法中直接实现,我们需要判断外部变量比如bool值来决定是否显示这个控件,但需要额外的代码写在tableView:cellForRowAtIndexPath:方法当中,如果我们把bool值传递给该cell让其自己判断是否显示隐藏这个控件,可读性将会大幅增加:) 效果: 源码: YXCell.h // // YXCell.

在UITableView中识别作用滑动,实现上下翻页的功能

目前有三种方案: 1. UIScrollView + UITableView. 实现方法,在UIScrollView中,加入UITableView即可 设置UIScrollView的代理和方法 - (void)scrollViewDidScroll:(UIScrollView *)scrollView{ int currentPostion = scrollView.contentOffset.x; if (currentPostion - 0 > 50) { NSLog(@"Scroll

iOS UITableView中异步加载图片 - 解决方案

问题背景: 需要在UITableView中的每一行下载图片,之前使用placeholder,下载好后存在cache中. 解决方案: 方案一: 使用SDWebImage:https://github.com/rs/SDWebImage 如何安装及使用在git页面有详细解释,具体使用的代码: #import <SDWebImage/UIImageView+WebCache.h> ... - (UITableViewCell *)tableView:(UITableView *)tableView

UITableView中识别左右滑动,实现上下翻页的功能

目前有三种方案: 1. UIScrollView + UITableView. 实现方法,在UIScrollView中,加入UITableView即可 设置UIScrollView的代理和方法 - (void)scrollViewDidScroll:(UIScrollView *)scrollView{ int currentPostion = scrollView.contentOffset.x; if (currentPostion - 0 > 50) { NSLog(@"Scroll

UI细节点滴-UIScorllView和UITableView/UITextView拖动退出键盘方法

在实际开发中,我们常常会用到UIScrollView以及它的子类控件UITableView/UITextView来进行某些操作需要唤出键盘.通常调出键盘的方式是使相应的控件成为事件的第一响应者,例如在一个视图加载完成后的ViewDidLoad方法中,调出键盘: - (void)viewDidAppear:(BOOL)animated {   [super viewDidAppear:animated];   [self.textView becomeFirstResponder];    //

UITableView中headerView视察滚动的简单实现

简单思路:实例一个UIScrollView,在scrollView上添加两个UIView, 为scrollView添加观察者,观察scrollView的contentOffset属性. 当偏移量改变时,改变UIView视图的坐标. 示例代码: @interface RootViewController () @property (nonatomic, copy) UIScrollView *scrollView; @property (nonatomic, copy) UIView *headV

UITableView中Cell和section的插入与删除

插入段: - (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation; - (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation; 插入行: - (void)insertRowsAtIndexPaths:(NSArray *)

schema中的虚拟属性方法

schema中的虚拟属性方法相当于vue中的计算属性,它是通过已定义的schema属性的计算\组合\拼接得到的新的值 var personSchema = new Schema({ name: { first: String, last: String } }); var Person = mongoose.model('Person', personSchema); // create a document var bad = new Person({ name: { first: 'Walt