0

我试图将两种不同UICollectionViewCells的东西合二为一UICollectionView。它正在工作,但经过一些滚动后,我收到内存警告并且应用程序崩溃。

这是我的代码:

创建并填充单元格:

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"KatalogBAGCell";
    BAGCell *cell = (BAGCell*)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

    id unknown = [[self.katalogBAGs objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

    BAGObject* bag = nil;
    SerienObject* serie = nil;
    NSString *filePath;

    if ([unknown isKindOfClass:[SerienObject class]]) {
        serie = (SerienObject*)unknown;
        cell = nil;
        cell = [[BAGCell alloc] initWithSerie:serie.serienID];


        filePath = serie.completePath;

        cell.bagText.text = serientext;
        cell.directText.text = serie.serienName;
        [cell.directText sizeToFit];
    }
    else {
        bag = (BAGObject*)unknown;

        if ( [self isEmptyCell:bag.fileName] ) {
            cell.alpha = 0.0;
            cell.hidden = cell.contentView.hidden = YES;

            return cell;
        }
        else {
            cell.alpha = 1.0;
            cell.hidden = cell.contentView.hidden = NO;


            // should be a real bag
            if (self.viewMode==0) {
                // regular floating mode
                cell.fullscreenButton.hidden = NO;
                [cell.bookmarkButton addTarget:self action:@selector(createBookMarkForThisBAG:) forControlEvents:UIControlEventTouchUpInside];
                [cell.fullscreenButton addTarget:self action:@selector(pushToFullScreenView:) forControlEvents:UIControlEventTouchUpInside];
            }
            else if (self.viewMode==1) {
                // fullscreen mode
                cell.fullscreenButton.hidden = YES;
                [cell.bookmarkButton addTarget:self action:@selector(createBookMarkForThisBAG:) forControlEvents:UIControlEventTouchUpInside];
            }

            filePath = bag.completePath;

            // clear the reused cell
            for (UIView *v in [cell.directOrderContainer subviews]) {
                if ((UILabel*)v==cell.directText) {
                    continue;
                }
                [v removeFromSuperview];
            }


            if (SAMOADelegate.appIsInPresentationMode==NO) {
                // generate the infos for the textInfoLine

                NSString *artCols = @"a.vkpreis, a.ve, a.groesse, a.ausverkauft";
                NSString *sqlStatement = [NSString stringWithFormat:@"SELECT %@ FROM kataloge_bags_artikel as ba LEFT JOIN artikel as a ON a.artikel = ba.artikelnr  where ba.bagid=%i order by ba.position_in_bag ASC", artCols, bag.bagID];
                sqlite3_stmt *itemSQL;
                NSString *lineText = @"";
                int c = 0;
                if(sqlite3_prepare_v2([ToolBox db], [sqlStatement UTF8String], -1, &itemSQL, NULL) == SQLITE_OK) {
                    while(sqlite3_step(itemSQL) == SQLITE_ROW) {

                        ArticleItem *item = [[ArticleItem alloc] init];

                        item.vkpreis = [[NSDecimalNumber alloc] initWithFloat:[self.helper calculateArticlePriceforPrice:sqlite3_column_double(itemSQL, 0)]];
                        item.verpackungseinheit = sqlite3_column_int(itemSQL, 1);
                        item.sizeInCM = [ToolBox convertFloatToString:sqlite3_column_double(itemSQL, 2)];
                        item.ausverkauft = [ToolBox checkNullString:(char *)sqlite3_column_text(itemSQL, 3)];

                        NSString *trenner = @"";
                        if (c!=0) {
                            trenner = @" · ";
                        }
                        if (item.ausverkauft.length!=0) {
                            // ausverkauft
                            NSString *price = [NSString stringWithFormat:@"- %@", SAMOADelegate.loggedInCustomer.waehrungsZeichen];
                            NSString *part = [NSString stringWithFormat:@"%@%@  %@:%i  %@ %@", trenner, price, veText, item.verpackungseinheit, item.sizeInCM, cmText];
                            lineText = [lineText stringByAppendingString:part];
                        }
                        else {
                            // not ausverkauft
                            NSString *price = [ToolBox convertDecimalNumber: item.vkpreis withCurrencySymbol:SAMOADelegate.loggedInCustomer.waehrungsZeichen];
                            NSString *part = [NSString stringWithFormat:@"%@%@  %@:%i  %@ %@", trenner, price, veText, item.verpackungseinheit, item.sizeInCM, cmText];
                            lineText = [lineText stringByAppendingString:part];
                        }

                        c++;

                    }
                } else { [ToolBox logDatabaseError:_cmd]; } sqlite3_finalize(itemSQL);


                bag.textInfoLine = lineText;

            }

            // check if this an article which you can order directly
            if (bag.amountOfArticles==1) {
                [self createDirectOrderViewForCell:cell andItem:bag];
                [cell.directText setText:bag.marketingtext];
            }
            else {
                cell.defaultContainer.hidden = NO;
                cell.directOrderContainer.hidden = YES;
                if (SAMOADelegate.appIsInPresentationMode==YES) {
                    [cell.bagText setText:bag.marketingtext];
                }
                else {
                    [cell.bagText setText:bag.textInfoLine];
                }
            }

            if (bag.isBookMarked==YES) {
                [cell.bookmarkButton setImage:[UIImage imageNamed:@"bookmark-icon-active.png"] forState:UIControlStateNormal];
            }
            else {
                [cell.bookmarkButton setImage:[UIImage imageNamed:@"bookmark-icon-inactive.png"] forState:UIControlStateNormal];
            }

            if (SAMOADelegate.appIsInPresentationMode==YES) {
                cell.bookmarkButton.hidden = YES;
            }

            // check if this bag is already in the cart
            int artInCart = [ToolBox returnSQLCountForQuery:[NSString stringWithFormat:@"SELECT COUNT(*) FROM auftragsposition where auftragsnr = '%i' AND bagid = '%i' LIMIT 1", SAMOADelegate.currentOrder.belegnr, bag.bagID]];

            if (artInCart) {
                cell.isInCartStateView.image = [UIImage imageNamed:@"ordered-dot.png"];
            }
            else {
                cell.isInCartStateView.image = [UIImage imageNamed:@"not-ordered-dot.png"];
            }
            self.lastLoadedBagIs  = bag.bagID;
            self.latestLoadedIndex = indexPath.row;

        }
    }

    __weak UIActivityIndicatorView *iV = cell.loadingIndicator;
    [cell.loadingIndicator startAnimating];
    [cell.bagImageView setImageWithURL:[NSURL fileURLWithPath:filePath]
                      placeholderImage:nil
                               options:0
                              progress:^(NSUInteger receivedSize, long long expectedSize) { [iV startAnimating]; }
                             completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) { [iV stopAnimating];
                             }];


    //rounded corners for the cell
    [self roundedCornersForLayerinCell:cell.bagImageView.layer];
    [cell.bagImageView.layer setShadowPath:[[UIBezierPath bezierPathWithRoundedRect:cell.frame cornerRadius:cell.layer.cornerRadius] CGPath]];
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:cell.bounds byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake(10.0, 10.0)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = cell.bounds;
    maskLayer.path = maskPath.CGPath;

    // Set the newly created shape layer as the mask for the image view's layer
    cell.layer.mask = maskLayer;

    return cell;
}

和单元格对象*.m

-(id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {

        // Initialization code
        NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"BAG-Template-1" owner:self options:nil];

        if (arrayOfViews.count < 1) {
            return nil;
        }
        if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]]) {
            return nil;
        }
        self = [arrayOfViews objectAtIndex:0];


    }
    return self;
}

和辅助初始化单元:

-(id)initWithSerie:(int)serienID {
    self = [super init];
    if (self) {
        if (serienID>0) {
            // Initialization code
            NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"BAG-Template-2" owner:self options:nil];

            if (arrayOfViews.count < 1) {
                return nil;
            }
            if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]]) {
                return nil;
            }
            self = [arrayOfViews objectAtIndex:0];
        }
        else {
            // Initialization code
            NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"BAG-Template-1" owner:self options:nil];

            if (arrayOfViews.count < 1) {
                return nil;
            }
            if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]]) {
                return nil;
            }
            self = [arrayOfViews objectAtIndex:0];
        }

    }
    return self;
}

知道为什么我会收到内存警告吗?

4

0 回答 0