4

我有一个非常密集的 UITableView 需要稍微优化一下。问题是,如何利用大中央车站有效地做到这一点。每个单元格都有一个带有几个标签和两个图像的 UIView。我已经对 TableViewCell 进行了子类化,并且视图正在被重用,尽管当表变大时它仍然有点滞后。我将如何使用 GCD 优化表?或者有没有更好的解决方法?我在线程管理和寻找一些建议方面不是很强。

这是我的表格视图的代码:

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

static NSString *CellIdentifier = @"Cell";
JointCAD *currentCall = [[xmlParser calls] objectAtIndex:indexPath.row];
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"texture3.png"]];

TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
{
    cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

cell.callTypeLabel.text = currentCall.currentCallType;
cell.locationLabel.text = currentCall.location;
cell.unitsLabel.text = currentCall.units;
cell.stationLabel.text = [@"Station: " stringByAppendingString:currentCall.station];
cell.selectedBackgroundView = cell.selectionView;

if ([currentCall.callType isEqualToString:@"F"]) {
    cell.imageType = Fire;
}
else {
    cell.imageType = EMS;
}

if ([currentCall.county isEqualToString:@"W"]) {
    cell.imageType1 = Washington;
}
else {
    cell.imageType1 = Clackamas;
}

return cell;
}

这是子类的tableviewcell:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

    callView = [[UIView alloc] initWithFrame:CGRectMake(7.5, 7, 305, 65)];
    [callView setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin |
     UIViewAutoresizingFlexibleRightMargin |
     UIViewAutoresizingFlexibleWidth];
    [callView setContentMode:UIViewContentModeTopLeft];
    [callView setBackgroundColor: [UIColor colorWithRed:240.0/255.0 green:240.0/255.0 blue:240.0/255.0 alpha:1.0]];
    callView.layer.borderWidth = 1.0;
    callView.layer.borderColor = [UIColor colorWithRed:(0/255.0)  green:(0/255.0)  blue:(0/255.0)  alpha:1.0].CGColor;

    [self.contentView addSubview:callView];

    callTypeLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 2, 190, 21)];
    callTypeLabel.font = [UIFont boldSystemFontOfSize:12.0];
    callTypeLabel.textColor = [UIColor blackColor];
    callTypeLabel.backgroundColor = [UIColor clearColor];
    callTypeLabel.highlightedTextColor = [UIColor whiteColor];
    callTypeLabel.adjustsFontSizeToFitWidth = YES;
    [callView addSubview:callTypeLabel];

    locationLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 17 , 190, 15)];
    locationLabel.font = [UIFont systemFontOfSize:10.0];
    locationLabel.textColor = [UIColor blackColor];
    locationLabel.backgroundColor = [UIColor clearColor];
    locationLabel.highlightedTextColor = [UIColor whiteColor];
    locationLabel.adjustsFontSizeToFitWidth = YES;
    [callView addSubview:locationLabel];

    unitsLabel = [[UILabel alloc]initWithFrame:CGRectMake(4, 43, 190, 21)];
    unitsLabel.font = [UIFont systemFontOfSize:10.0];
    unitsLabel.textColor = [UIColor blackColor];
    unitsLabel.backgroundColor = [UIColor clearColor];
    unitsLabel.highlightedTextColor = [UIColor whiteColor];
    unitsLabel.adjustsFontSizeToFitWidth = NO;
    [callView addSubview:unitsLabel];

    stationLabel = [[UILabel alloc]initWithFrame:CGRectMake(195 , 25, 75, 20)];
    stationLabel.font = [UIFont systemFontOfSize:12.0];
    stationLabel.textColor = [UIColor blackColor];
    stationLabel.backgroundColor = [UIColor clearColor];
    stationLabel.highlightedTextColor = [UIColor whiteColor];
    stationLabel.adjustsFontSizeToFitWidth = YES;
    [callView addSubview:stationLabel];

    CGRect countyImageFrame = CGRectMake(275, 10, 18, 18);
    UIImageView *countyImageView = [[UIImageView alloc] initWithFrame:countyImageFrame];
    countyImageView.image = countyImage;
    [callView addSubview:countyImageView];

    CGRect callTypeImageFrame = CGRectMake(275, 37, 18, 18);
    UIImageView *callTypeImageView = [[UIImageView alloc] initWithFrame:callTypeImageFrame];
    callTypeImageView.image = callTypeImage;
    [callView addSubview:callTypeImageView];

    selectionView = [[UIView alloc] initWithFrame:CGRectMake(10, 7, 200, 65)];
    [selectionView setBackgroundColor: [UIColor clearColor]];

    }

    return self;
}

- (void)setImageType:(CallType)newImageType {
imageType = newImageType;

if (imageType == Fire) {
    CGRect callTypeImageFrame = CGRectMake(275, 37, 18, 18);
    UIImageView *callTypeImageView = [[UIImageView alloc] initWithFrame:callTypeImageFrame];
    callTypeImageView.image = [UIImage imageNamed:@"red.png"];
    [callView addSubview:callTypeImageView];
}
else if (imageType == EMS) {
    CGRect callTypeImageFrame = CGRectMake(275, 37, 18, 18);
    UIImageView *callTypeImageView = [[UIImageView alloc] initWithFrame:callTypeImageFrame];
    callTypeImageView.image = [UIImage imageNamed:@"yellow.png"];
    [callView addSubview:callTypeImageView];
    }
}

- (void)setImageType1:(County)newImageType1 {
imageType1 = newImageType1;

if (imageType1 == Washington) {
    CGRect callTypeImageFrame = CGRectMake(275, 10, 18, 18);
    UIImageView *countyImageView = [[UIImageView alloc] initWithFrame:callTypeImageFrame];
    countyImageView.image = [UIImage imageNamed:@"blue.png"];
    [callView addSubview:countyImageView];
}
else if (imageType1 == Clackamas) {
    CGRect callTypeImageFrame = CGRectMake(275, 10, 18, 18);
    UIImageView *countyImageView = [[UIImageView alloc] initWithFrame:callTypeImageFrame];
    countyImageView.image = [UIImage imageNamed:@"green.png"];
    [callView addSubview:countyImageView];
    }
}
4

1 回答 1

3

这有点微妙,但您的代码将停留的主要区域是 setImageType: 方法。

您在此处将一个以编程方式创建的图像视图添加到您的视图层次结构中:

UIImageView *callTypeImageView = [[UIImageView alloc] initWithFrame:callTypeImageFrame];
callTypeImageView.image = [UIImage imageNamed:@"red.png"];
[callView addSubview:callTypeImageView];

但是您永远不会真正删除旧的图像视图。更好的方法可能是将创建的图像视图缓存在单元格的属性中,然后在设置图像类型时,在创建新图像视图之前将消息 -[UIView removeFromSuperview] 发送到旧图像视图。

正如您现在的代码所示,每次出列单元格时,都会向其中添加一个新的图像视图,因此每次用户上下滚动表格视图时,都会创建一个新的图像视图并将其添加到单元格中。每个单元格中很快就会有几十个图像视图。我怀疑这会导致对图像视图的 drawRect 调用比实现您的目的实际需要的多很多倍。

更好的方法是将这两种类型的图像视图作为您在单元格的 init 方法中创建的属性,这些属性仅在 setType 方法中配置。这样,您只需为每种类型创建一个图像视图,并且只需在适当的 setType 方法中设置配置其图像。这样做,请记住 removeFromSuperview 将释放图像视图,因此您必须将其声明为强属性(假设您使用的是 ARC)。

我很欣赏这些解决方案都与 Grand Central Dispatch 无关,但希望这可以解决您的问题,而无需使用大锤来破解 :)。

于 2012-12-05T11:43:05.800 回答