1

tableView:CellForRowAtIndexpath 内でプログラムで作成している UItableViewCell があり、しばらくスクロールした後にパフォーマンスが低下しています。

私は当初、InterfaceBuilder内のカスタムUITableViewCellとしてそれを持っていましたが、プログラムでこのビューを作成したことはありません。これにより、パフォーマンスが約40%向上しましたが、40〜100行スクロールした後、非常にグリッチが発生し始めました..セルの UITableView の高さをキャッシュする方法を見つけましたが、コードの根本的なエラーである場合、私は今傷ついています。たとえば、私の両端キューが機能していませんか? これを確認する方法がわかりません。問題の診断に役立つことを期待していました。

これは私のテーブルビューメソッドがどのように見えるかです

- (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のスクロールパフォーマンスを解決するアイデアや助けをいただければ幸いです。

4

1 に答える 1

1

次のようなもの: [cell addSubview:areaLabel];(where areaLabel = [[UILabel alloc] init..) は、 内cellForRowAtIndexPathで実行しない限り、 内では使用できませんif (cell == nil)。あなたの場合、ラベルを作成してセルに継続的に追加しているため、しばらくスクロールした後、実際のセルには多数のラベル/サブビューが含まれます(一部は他の背後に隠れている可能性があります)。

これらのサブビューを一度だけ作成して追加する必要があり、その後はコンテンツを設定/更新するだけです。

@Fogmeisterが述べたように、代わりにカスタムセルを作成するのが最善です(すべてのカスタマイズを行う別のクラス)。

于 2013-10-22T09:15:20.417 に答える