iOS GCD用于UITableView

iOS GCD用于UITableView,第1张

概述我有一个非常密集的UITableView,需要进行一些优化.问题是,如何使用大中央站有效地做到这一点.每个单元格都有一个带有几个标签和两个图像的UIView.我已经将TableViewCell子类化了,并且视图正在被重用,尽管当表变大时它仍然有点滞后.我将如何使用GCD优化表格?或者有更好的方法吗?我在线程管理方面不是很强,并且正在寻找一些建议. 这是我的tableview的代码: - (UITa 我有一个非常密集的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所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1038457.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-24
下一篇2022-05-24

发表评论

登录后才能评论

评论列表(0条)

    保存