表格不复用,来回上下来动表格,轻者造成生成大量的表格,内存暴增,容易产生异常。重者造成表格内容重复,看到你不期望出现的内容,复杂表格(自适应高度的表格)高度计算不准。
1.简单的表格直接使用dequeueReusableCellWithIdentifier来进行常规表格复用。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
AboutCell *cell = [tableView dequeueReusableCellWithIdentifier:CellWithIdentifier];
if (!cell) {
cell = [[AboutCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellWithIdentifier];
}
cell.titleLabel.text = textArray[indexPath.row];
return cell;
}
2.通过对表格设置唯一标识符来复用表格,就是出现过的表格重用,没有加载过的表格不重用。通常以消息号加消息类型来作为表格唯一标识。
能解决,根据不同的内容显示不同的内容,自适应高度的表格。
这种方式,表格重用率很低。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *str = [self getIdentifier:indexPath];
if(str.length == 0)
{
str = @"SectionHead";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
}
cell.backgroundColor = BACKGROUND_COLOR;
return cell;
}
if(indexPath.row % 2 == 0)
{
BOOL isDisplaySection = [self getIsDisplaySection:indexPath];
if(isDisplaySection)
{
return [self getMessageDateSectionCellWithIdentifier:str indexPath:indexPath];
}
else
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
}
cell.backgroundColor = BACKGROUND_COLOR;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
}
else
{
MESSAGE_TYPE messageType = [self getMessageType:indexPath];
if((messageType == MESSAGE_TYPE_PAY) || (messageType == MESSAGE_TYPE_TASK))
{
return [self getMessageCell1WithIdentifier:str indexPath:indexPath];
}
else
{
return [self getMessageCellWithIdentifier:str indexPath:indexPath];
}
}
}
-(NSString *)getIdentifier : (NSIndexPath *)indexPath
{
if(indexPath.section < _modelsArray.count)
{
MessageCellModel *cellModel = (MessageCellModel *)(self.modelsArray[indexPath.section][indexPath.row]);
return [NSString stringWithFormat:@"%d:%@", (int)(cellModel.messageType), cellModel.id];
}
else if([[SingleObject sharedInstance].locationMessagesArray isKindOfClass:[NSMutableArray class]] && ([SingleObject sharedInstance].locationMessagesArray.count > indexPath.section))
{
MessageCellModel *cellModel = (MessageCellModel *)([SingleObject sharedInstance].locationMessagesArray[indexPath.section][indexPath.row]);
return [NSString stringWithFormat:@"%d:%@", (int)(cellModel.messageType), cellModel.id];
}
else
{
return nil;
}
}
3.既然自适应高度根据内容来显示完全不同的内容的表格,那么复用本表格,完全不复用其上的内容,直接干掉重用过来的表格上的内用,对复用的这个表格进行数据配置既可。
当页面拉动的时候 当cell存在并且最后一个存在 把它进行删除就出来一个独特的cell我们在进行数据配置即可避免
这种方式表格复用率很高,能解决复杂表格显示问题。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
GBTripManageCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:cellWithIdentifier];
if (!cell) {
cell = [[GBTripManageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellWithIdentifier];
}
else//当页面拉动的时候 当cell存在并且最后一个存在 把它进行删除就出来一个独特的cell我们在进行数据配置即可避免
{
while ([cell.contentView.subviews lastObject] != nil) {
[(UIView *)[cell.contentView.subviews lastObject] removeFromSuperview];
}
cell = [[GBTripManageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellWithIdentifier];
}
if([self.tripManageEntity.orderList isKindOfClass:[NSMutableArray class]] && (self.tripManageEntity.orderList.count > 0))
{
[self setCellModelWithGBTripManageCell:cell orderList:self.tripManageEntity.orderList tableView:tableView indexPath:indexPath];
}
return cell;
}