IOS应用程序在UITableView滚动时崩溃

时间:2013-11-16 19:40:40

标签: ios uitableview crash scroll

我的桌子有问题。当我向下滚动一切 没问题,但是当我向上滚动我的应用程序时。我在这里检查了很多答案,但似乎没有人帮助我。这是我创建单元格的代码。当试图调试时,它崩溃了:

   NewProductSmallCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

但为什么??

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellIdentifier = @"cell";
NSString *cellIdentifier1 = @"cell1";

if(indexPath.row >= [[ProductList instance].productList count]){
    NSLog(@"ROW IS > THAN LIST SIZE: %d > %d", indexPath.row, [[ProductList instance].productList count]);
}

if(indexPath.row == 0){
    NewProductSmallCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(!cell){
        cell = [[NewProductSmallCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    return cell;
}else{
    if((indexPath.row % 2) != 0){
        NewProductBigCell *big = [tableView dequeueReusableCellWithIdentifier:cellIdentifier1];
        if(!big){
            big = [[NewProductBigCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier1];
        }

        big.choose = self;

        Product *p1 = nil;
        if(indexPath.row > 1){
            p1 = [[ProductList instance].productList objectAtIndex: (indexPath.row + countBig)];
            big.count = (indexPath.row - 1);
        }else{
            p1 = [[ProductList instance].productList objectAtIndex: indexPath.row];
            big.count = indexPath.row;
        }

        big.img.image = nil;
        big.price = 0.0;

        [big.labelBuy setTitle:@"" forState:UIControlStateNormal];
        big.price = [p1.price doubleValue];

        NSString *uri = [NSString stringWithFormat:@""];

        if(([p1.username length] == 0) || ([p1.directory length] == 0) || ([p1.fileName length] == 0)){
            big.img.image = [UIImage imageNamed:@"btn1.png"];
        }else{
            [Utils downloadImageWithURL:[NSURL URLWithString:uri] completionBlock:^(BOOL succeeded, UIImage *image) {
                if (succeeded) {
                    big.img.image = image;
                    p1.image = image;
                }
            }];
        }
        countBig ++;
        return big;
    }else{
        if(indexPath.row != 0){
            NSString *smallCellID = @"smallCell";
            ProductSmallCell *smallCell = [tableView dequeueReusableCellWithIdentifier:smallCellID];
            if(!smallCell){
                smallCell = [[ProductSmallCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:smallCellID];
            }

            [smallCell.imgLeft setBackgroundImage:nil forState:UIControlStateNormal];
            [smallCell.imgRight setBackgroundImage:nil forState:UIControlStateNormal];
            smallCell.priceLeft = 0.0;
            smallCell.posLeft = 0;

            smallCell.priceRight = 0.0;
            smallCell.posRight = 0;

            if((indexPath.row + countSmall) < [[ProductList instance].productList count]){
                Product *pLeft = [[ProductList instance].productList objectAtIndex:(indexPath.row + countSmall)];

                [smallCell.imgLeft setBackgroundImage:nil forState:UIControlStateNormal];

                NSString *uri = [NSString stringWithFormat:@""];

                [Utils downloadImageWithURL:[NSURL URLWithString:uri] completionBlock:^(BOOL succeeded, UIImage *image) {
                    if (succeeded) {
                        [smallCell.imgLeft setBackgroundImage:image forState:UIControlStateNormal];
                        pLeft.image = image;
                    }
                }];

                smallCell.priceLeft = [[pLeft price] doubleValue];
                smallCell.posLeft = (countSmall);
            }
            if((indexPath.row + countSmall + 1) < [[ProductList instance].productList count]){
                Product *pRight = [[ProductList instance].productList objectAtIndex:(indexPath.row + countSmall + 1)];

                [smallCell.imgLeft setBackgroundImage:nil forState:UIControlStateNormal];

                NSString *uri = [NSString stringWithFormat:@""];

                [Utils downloadImageWithURL:[NSURL URLWithString:uri] completionBlock:^(BOOL succeeded, UIImage *image) {
                    if (succeeded) {
                        [smallCell.imgLeft setBackgroundImage:image forState:UIControlStateNormal];
                        pRight.image = image;
                    }
                }];

                smallCell.priceRight = [[pRight price] doubleValue];
                smallCell.posRight = (countSmall + 1);
            }
            countSmall ++;
            return smallCell;
        }
    }
}
return nil;

}

1 个答案:

答案 0 :(得分:0)

您似乎正在返回第0行和所有奇数行的单元格,但不会返回任何其他偶数行。 cellForRowAtIndexPath方法必须始终返回一个单元格,你不能返回nil,所以你需要为偶数行做一些事情。