HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了iOS GCD用于UITableView大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个非常密集的UITableView,需要进行一些优化.问题是,如何使用大中央站有效地做到这一点.每个单元格都有一个带有几个标签和两个图像的UIView.我已经将TableViewCell子类化了,并且视图正在被重用,尽管当表变大时它仍然有点滞后.我将如何使用GCD优化表格?或者有更好的方法吗?我在线程管理方面不是很强,并且正在寻找一些建议.

这是我的tableview的代码

- (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;
}

这是子类表viewcell:

- (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,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,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);
    UIImageView *callTypeImageView = [[UIImageView alloc] initWithFrame:callTypeImageFrame];
    callTypeImageView.image = callTypeImage;
    [callView addSubview:callTypeImageView];

    SELEctionView = [[UIView alloc] initWithFrame:CGRectMake(10,200,65)];
    [SELEctionView setBACkgroundColor: [UIColor clearColor]];

    }

    return self;
}

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

if (imageType == FirE) {
    CGRect callTypeImageFrame = CGRectMake(275,18);
    UIImageView *callTypeImageView = [[UIImageView alloc] initWithFrame:callTypeImageFrame];
    callTypeImageView.image = [UIImage imagenamed:@"red.png"];
    [callView addSubview:callTypeImageView];
}
else if (imageType == EMS) {
    CGRect callTypeImageFrame = CGRectMake(275,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,18);
    UIImageView *countyImageView = [[UIImageView alloc] initWithFrame:callTypeImageFrame];
    countyImageView.image = [UIImage imagenamed:@"blue.png"];
    [callView addSubview:countyImageView];
}
else if (imageType1 == Clackamas) {
    CGRect callTypeImageFrame = CGRectMake(275,18);
    UIImageView *countyImageView = [[UIImageView alloc] initWithFrame:callTypeImageFrame];
    countyImageView.image = [UIImage imagenamed:@"green.png"];
    [callView addSubview:countyImageView];
    }
}

解决方法

这有点微妙,但代码将挂起的主要区域是setImageType:方法.

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

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

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

正如您的代码现在所示,每次单元格出列时,都会向其添加一个新的图像视图,因此每次用户在表格视图中上下滚动时,都会创建一个新的图像视图并将其添加到单元格中.每个单元格中有数十个图像视图不会花费很长时间.我怀疑这导致了多次drawRect调用到图像视图,而不是实现你的目的所需的.

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

我很欣赏这些解决方案都没有与Grand Central Dispatch有任何关系,但希望这可以解决你的问题,而不需要使用大锤来破解坚果:).

大佬总结

以上是大佬教程为你收集整理的iOS GCD用于UITableView全部内容,希望文章能够帮你解决iOS GCD用于UITableView所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。