簡體   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