1

我有一个 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 的滚动性能将不胜感激。

4

1 回答 1

1

诸如:[cell addSubview:areaLabel];(where areaLabel = [[UILabel alloc] init..) 在里面是不行的cellForRowAtIndexPath,除非你在里面做if (cell == nil)。在您的情况下,您正在为单元格连续创建和添加标签,因此在滚动一段时间后,您的实际单元格将包含大量标签/子视图(有些可能会隐藏在其他单元格后面)。

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

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

于 2013-10-22T09:15:20.417 回答