繁体   English   中英

滚动一段时间后,UItableView性能问题

[英]UItableView performance issue after scrolling for a while

我有一个UItableViewCell,它是在tableView:CellForRowAtIndexpath内以编程方式创建的,在滚动了一段时间后,性能下降。

我最初将它作为InterfaceBuilder中的自定义UITableViewCell使用,但是我没有以编程方式创建此视图,该视图将性能提高了约40%,但是在滚动显示40-100行后,它开始变得非常毛刺。出了如何缓存单元格UITableView的高度的方法,但是如果我的代码中出现基本错误,我现在就很痛苦。 例如mybe我的双端队列不起作用? 我不确定如何检查此问题,并希望在诊断我的问题时获得帮助。

这就是我的tableview方法的样子

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

    // Configure the cell...
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if ([sortedItemsArray count] > 0) {
        NSDictionary *currentInstallDictionary = [sortedItemsArray objectAtIndex:indexPath.row];

        NSNumber *tempDUP = [currentInstallDictionary objectForKey:@"dup"];
        NSInteger myInteger = [tempDUP integerValue];

        if (myInteger == 0) {
            //assign vals to labels
            areaLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0, -2.0, 140.0, 35.0)];
            NSString *areaString = [currentInstallDictionary objectForKey:@"area"];
            if ((NSNull *) areaString != [NSNull null]) {
                areaLabel.text = areaString;
            } else {
                areaLabel.text = @" ";
            }
            areaLabel.lineBreakMode = NSLineBreakByCharWrapping;
            areaLabel.numberOfLines = 0;
            [areaLabel sizeToFit];
            [cell addSubview:areaLabel];

            // removed other UIlabel that were here for readability

            descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(450.0, 5.0, 140.0, 35.0)];
            NSString *descriptionString = [currentInstallDictionary objectForKey:@"partDesc"];
            if ((NSNull *) descriptionString != [NSNull null]) {
                descriptionLabel.text = descriptionString;
            } else {
                descriptionLabel.text = @" ";
            }
            descriptionLabel.lineBreakMode = NSLineBreakByCharWrapping;
            descriptionLabel.numberOfLines = 0;
            [descriptionLabel sizeToFit];
            [cell addSubview:descriptionLabel];

            shipmentLabel = [[UILabel alloc] initWithFrame:CGRectMake(605.0, 5.0, 140.0, 35.0)];
            NSString *shipString = [currentInstallDictionary objectForKey:@"totShip"];
            if ((NSNull *) shipString != [NSNull null]) {
                 shipmentLabel.text = shipString;
            } else {
                shipmentLabel.text = @" ";
            }
            shipmentLabel.lineBreakMode = NSLineBreakByCharWrapping;
            shipmentLabel.numberOfLines = 0;
            [shipmentLabel sizeToFit];
            [cell addSubview:shipmentLabel];

            quantityALabel = [[UILabel alloc] initWithFrame:CGRectMake(755.0, 5.0, 50.0, 35.0)];
            NSString *totDryQtyString = [currentInstallDictionary objectForKey:@"totDryQty"];
            if ((NSNull *) totDryQtyString != [NSNull null]) {
                quantityALabel.text = totDryQtyString;
            } else {
                quantityALabel.text = @" ";
            }
            quantityALabel.lineBreakMode = NSLineBreakByCharWrapping;
            quantityALabel.numberOfLines = 0;
            [quantityALabel sizeToFit];
            [cell addSubview:quantityALabel];

            quantityBLabel = [[UILabel alloc] initWithFrame:CGRectMake(815.0, 5.0, 50.0, 35.0)];
            NSString *totInsQtyString = [currentInstallDictionary objectForKey:@"totInsQty"];
            if ((NSNull *) totInsQtyString != [NSNull null]) {
                quantityBLabel.text = totInsQtyString;
            } else {
                quantityBLabel.text = @" ";
            }
            quantityBLabel.lineBreakMode = NSLineBreakByCharWrapping;
            quantityBLabel.numberOfLines = 0;
            [quantityBLabel sizeToFit];
            [cell addSubview:quantityBLabel];




            NSString *tDStateAString = [currentInstallDictionary objectForKey:@"totDryStateA"];
            NSString *tDStateBString = [currentInstallDictionary objectForKey:@"totDryStateB"];

            fitButtonImage = [UIButton buttonWithType:UIButtonTypeCustom];
            fitButtonImage.frame = CGRectMake(888.0, 5.0, 35.0, 35.0);
            if (([tDStateAString isEqualToString:@"T"]) && ([tDStateBString isEqualToString:@"W"])) {
                UIImage *checkedOnImage = [UIImage imageNamed:@"CheckedOn.png"];
                [fitButtonImage setImage:checkedOnImage forState:UIControlStateNormal];
                [cell addSubview:fitButtonImage];
            } else if (([tDStateAString isEqualToString:@"T"]) && ([tDStateBString isEqualToString:@"R"])) {
                UIImage *checkedOnDisabledImage = [UIImage imageNamed:@"CheckedOnDisabled.png"];
                [fitButtonImage setImage:checkedOnDisabledImage forState:UIControlStateNormal];
                [cell addSubview:fitButtonImage];
            } else {
                if (![quantityALabel.text isEqualToString:@"0"]) {
                    UIImage *checkedOffImage = [UIImage imageNamed:@"CheckedOff.png"];
                    [fitButtonImage setImage:checkedOffImage forState:UIControlStateNormal];
                    fitButtonImage.userInteractionEnabled = YES;
                } else {
                    fitButtonImage.userInteractionEnabled = NO;
                }

            }


            NSString *tIStateAString = [currentInstallDictionary objectForKey:@"totInstStateA"];
            NSString *tIStateBString = [currentInstallDictionary objectForKey:@"totInstStateB"];

            insButtonImage = [UIButton buttonWithType:UIButtonTypeCustom];
            insButtonImage.frame = CGRectMake(968.0, 5.0, 35.0, 35.0);
            if (([tIStateAString isEqualToString:@"T"]) && ([tIStateBString isEqualToString:@"W"])) {
                UIImage *checkedOnImage = [UIImage imageNamed:@"CheckedOn.png"];
                [insButtonImage setImage:checkedOnImage forState:UIControlStateNormal];
                [cell addSubview:insButtonImage];
            } else if (([tIStateAString isEqualToString:@"T"]) && ([tIStateBString isEqualToString:@"R"])) {
                UIImage *checkedOnDisabledImage = [UIImage imageNamed:@"CheckedOnDisabled.png"];
                [insButtonImage setImage:checkedOnDisabledImage forState:UIControlStateNormal];
                [cell addSubview:insButtonImage];
            } else {
                UIImage *checkedOffImage = [UIImage imageNamed:@"CheckedOff.png"];
                [insButtonImage setImage:checkedOffImage forState:UIControlStateNormal];
                [cell addSubview:insButtonImage];
            }


            //add lines to cell
            lineBreakOneView = [[UIView alloc] initWithFrame:CGRectMake(140.0, 0.0, 0.5, 44.0)];
            lineBreakOneView.backgroundColor = [UIColor lightGrayColor];
            [cell addSubview:lineBreakOneView];

            lineBreakTwoView = [[UIView alloc] initWithFrame:CGRectMake(290.0, 0.0, 0.5, 44.0)];
            lineBreakTwoView.backgroundColor = [UIColor lightGrayColor];
            [cell addSubview:lineBreakTwoView];

            lineBreakThreeView = [[UIView alloc] initWithFrame:CGRectMake(440.0, 0.0, 0.5, 44.0)];
            lineBreakThreeView.backgroundColor = [UIColor lightGrayColor];
            [cell addSubview:lineBreakThreeView];

            lineBreakFourView = [[UIView alloc] initWithFrame:CGRectMake(590.0, 0.0, 0.5, 44.0)];
            lineBreakFourView.backgroundColor = [UIColor lightGrayColor];
            [cell addSubview:lineBreakFourView];

            lineBreakFiveView = [[UIView alloc] initWithFrame:CGRectMake(745.0, 0.0, 0.5, 44.0)];
            lineBreakFiveView.backgroundColor = [UIColor lightGrayColor];
            [cell addSubview:lineBreakFiveView];

            lineBreakSixView = [[UIView alloc] initWithFrame:CGRectMake(805.0, 0.0, 0.5, 44.0)];
            lineBreakSixView.backgroundColor = [UIColor lightGrayColor];
            lineBreakSixView.alpha = 0.3;
            [cell addSubview:lineBreakSixView];

            lineBreakSevenView = [[UIView alloc] initWithFrame:CGRectMake(865.0, 0.0, 0.5, 44.0)];
            lineBreakSevenView.backgroundColor = [UIColor lightGrayColor];
            [cell addSubview:lineBreakSevenView];

            lineBreakEightView = [[UIView alloc] initWithFrame:CGRectMake(945.0, 0.0, 0.5, 44.0)];
            lineBreakEightView.backgroundColor = [UIColor lightGrayColor];
            lineBreakEightView.alpha = 0.3;
            [cell addSubview:lineBreakEightView];

            return cell;
        }
    }
    return cell; // this shouldnt happen if the data is correct
}

实际上那里还有更多的标签..所以它是一个相当不错的UITableViewCell,我也想添加一些图像等等。但是在我这样做之前,我需要帮助来解决性能问题。正在遭受。

任何想法或帮助解决UITableView的滚动性能将不胜感激。

像这样的东西: [cell addSubview:areaLabel]; (其中areaLabel = [[UILabel alloc] init.. )在cellForRowAtIndexPath内部cellForRowAtIndexPath ,除非您在if (cell == nil) 在您的情况下,您要连续创建标签并将其添加到单元格,因此在滚动一会儿之后,您的实际单元格将包含大量标签/子视图(有些可能隐藏在其他标签/子视图之后)。

您只需要创建和添加这些子视图一次,然后仅设置/刷新它们的内容。

正如@Fogmeister提到的,最好创建一个自定义单元格(在其中进行所有自定义的单独类)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM