gpt4 book ai didi

iOS GCD 用于 UITableView

转载 作者:可可西里 更新时间:2023-11-01 05:46:13 25 4
gpt4 key购买 nike

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

这是我的表格 View 的代码:

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

最佳答案

这有点微妙,但您的代码将挂起的主要区域在 setImageType: 方法中。

您要在此处将以编程方式创建的 ImageView 添加到您的 View 层次结构中:

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

但您实际上并没有删除旧的 ImageView 。更好的方法可能是将创建的 ImageView 缓存在单元格的属性中,然后在设置图像类型时,在创建新 ImageView 之前将消息 -[UIView removeFromSuperview] 发送到旧 ImageView 。

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

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

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

关于iOS GCD 用于 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12752424/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com