今天做设备适配的时候遇到一个问题,一个有tableview的页面上,iphone5,iphone5s,iphone6,iphone6+展现都没有问题,但在iPhone4s上展现时发现tableview的最后一列出现内容重复的现象。数组
怎么会出现这种问题呢:iphone
因而咱们看看代码实现:ide
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPathspa
{orm
static NSString *cellIdentifier = @"UserInfoCell";ip
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];rem
if (cell == nil) {string
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];it
}io
if (indexPath.section == 0) {
if (indexPath.row == 0) {
cell.textLabel.text = @"头像";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
self.photoButton.frame = CGRectMake(self.view.frame.size.width - WIDTH_TO_LEFT_MARGIN- HEIGHT_OF_PHOTO, (HEIGHT_OF_PHOTO_CELL-HEIGHT_OF_PHOTO)/2, HEIGHT_OF_PHOTO, HEIGHT_OF_PHOTO);
UIImage *image = [UIImage imageWithContentsOfFile:self.photoGraphPath];
[self.photoButton setImage:image forState:UIControlStateNormal];
[cell.contentView addSubview:self.photoButton];
}
else if (indexPath.row == 1) {
cell.textLabel.text = @"名称";
cell.accessoryType = UITableViewCellAccessoryNone;
[self settingLabel:self.nameLabel];
[cell.contentView addSubview:self.nameLabel];
}...............
看下代码咱们分析一下就会知道,这是因为cell重用引发的,在另外几个设备上面基本上是一页或者一页多一点展现,所用到的cell都是直接初始化出来的,只有这个iphone4s超过一页显示,使用的是重用的cell,此cell上有老的数据没有remove。
因而咱们再把cell重用的机制梳理一下:
tableview的cell重用实现分析
查看UITableView头文件,会找到NSMutableArray* visiableCells,和NSMutableDictnery* reusableTableCells两个结构。visiableCells内保存当前显示的cells,reusableTableCells保存可重用的cells。
TableView显示之初,reusableTableCells为空,那么tableView dequeueReusableCellWithIdentifier:CellIdentifier返回nil。开始的cell都是经过[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]来建立,并且cellForRowAtIndexPath只是调用最大显示cell数的次数。
好比:有100条数据,iPhone一屏最多显示10个cell。程序最开始显示TableView的状况是:
1. 用[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]建立10次cell,并给cell指定一样的重用标识(固然,能够为不一样显示类型的cell指定不一样的标识)。而且10个cell所有都加入到visiableCells数组,reusableTableCells为空。
2. 向下拖动tableView,当cell1彻底移出屏幕,而且cell11(它也是alloc出来的,缘由同上)彻底显示出来的时候。cell11加入到visiableCells,cell1移出visiableCells,cell1加入到reusableTableCells。
3. 接着向下拖动tableView,由于reusableTableCells中已经有值,因此,当须要显示新的cell,cellForRowAtIndexPath再次被调用的时候,tableView dequeueReusableCellWithIdentifier:CellIdentifier,返回cell1。cell1加入到visiableCells,cell1移出reusableTableCells;cell2移出visiableCells,cell2加入到reusableTableCells。以后再须要显示的Cell就能够正常重用了。
因此整个过程并不难理解,但须要注意正是由于这样的缘由:配置Cell的时候必定要注意,对取出的重用的cell作从新赋值,不要遗留老数据。
一些状况
使用过程当中,我注意到,并非只有拖动超出屏幕的时候才会更新reusableTableCells表,还有:
1. reloadData,这种状况比较特殊。通常是部分数据发生变化,须要从新刷新cell显示的内容时调用。在cellForRowAtIndexPath调用中,全部cell都是重用的。我估计reloadData调用后,把visiableCells中全部cell移入reusableTableCells,visiableCells清空。cellForRowAtIndexPath调用后,再把reuse的cell从reusableTableCells取出来,放入到visiableCells。
2. reloadRowsAtIndex,刷新指定的IndexPath。若是调用时reusableTableCells为空,那么cellForRowAtIndexPath调用后,是新建立cell,新的cell加入到visiableCells。老的cell移出visiableCells,加入到reusableTableCells。因而,以后的刷新就有cell作reuse了。
看到这里咱们就基本熟悉和回忆起来了。那么怎么解决我这个问题呢:
1.cell原本就不是不少,能够不重用cell,每一个cell都是初始化出来的。
//static NSString *cellIdentifier = @"UserInfoCell";
NSString *cellIdentifier = [NSString stringWithFormat:@"Cell%d%d", [indexPath section], [indexPath row]];//每一个cell的identifier都不一样,因此不会重用
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
2.采用重用,重取出来的cell是有可能已经捆绑过数据或者加过子视图的,因此,若是有必要,要清除数据(好比textlabel的text)和remove掉add过的子视图。
static NSString *cellIdentifier = @"UserInfoCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
else
{
for (UIView* view in cell.contentView.subviews)
{
[view removeFromSuperview];
}
}
运行一下,发现问题解决了。