滚动表视图时应用程序崩溃

时间:2011-06-23 19:47:48

标签: objective-c cocoa-touch ipad uitableview uiimageview

在我的应用程序中,我有一个显示图像视图的表视图,一行中的4个图像视图。我的问题是在加载表视图后有一些数据然后如果我尝试滚动表视图然后我的应用程序崩溃。在使用断点时,我发现索引路径为methid的行的单元格再次被调用,我的数组再次尝试重新加载数据。请帮助我现在开始,我在这里发布我的代码: -

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = nil;



    static NSString *AutoCompleteRowIdentifier = @"AutoCompleteRowIdentifier";

    cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];

    if (cell == nil) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier] autorelease];


        UIImageView * imageView1 = [[[UIImageView alloc] initWithFrame:CGRectMake(25, 4, 70, 80)] autorelease];

        UIImageView * imageView2 = [[[UIImageView alloc] initWithFrame:CGRectMake(115,4,70, 80)] autorelease];

        UIImageView * imageView3 = [[[UIImageView alloc] initWithFrame:CGRectMake(205,4, 70, 80)] autorelease];

        UIImageView * imageView4 = [[[UIImageView alloc] initWithFrame:CGRectMake(295,4, 70, 80)] autorelease];

        UIImageView * imageView5 = [[[UIImageView alloc] initWithFrame:CGRectMake(25, 4, 70, 80)] autorelease];

        UIImageView * imageView6 = [[[UIImageView alloc] initWithFrame:CGRectMake(115,4,70, 80)] autorelease];

        UIImageView * imageView7 = [[[UIImageView alloc] initWithFrame:CGRectMake(205,4, 70, 80)] autorelease];

        UIImageView * imageView8 = [[[UIImageView alloc] initWithFrame:CGRectMake(295,4, 70, 80)] autorelease];

        UIImageView * imageView9 = [[[UIImageView alloc] initWithFrame:CGRectMake(25, 4, 70, 80)] autorelease];

        UIImageView * imageView10 = [[[UIImageView alloc] initWithFrame:CGRectMake(115,4,70, 80)] autorelease];

        UIImageView * imageView11 = [[[UIImageView alloc] initWithFrame:CGRectMake(205,4, 70, 80)] autorelease];

        UIImageView * imageView12 = [[[UIImageView alloc] initWithFrame:CGRectMake(295,4, 70, 80)] autorelease];



        imageView1.tag=1;
        imageView2.tag=2;
        imageView3.tag=3;
        imageView4.tag=4;
        imageView5.tag=5;
        imageView6.tag=6;
        imageView7.tag=7;
        imageView8.tag=8;
        imageView9.tag=9;
        imageView10.tag=10;
        imageView11.tag=11;
        imageView12.tag=12;



        if ([sentence count]>0) {

            [imageViewArray insertObject:imageView1 atIndex:0];

            [cell.contentView addSubview:imageView1];

        }

        if ([sentence count]>1) {

            [imageViewArray insertObject:imageView2 atIndex:1];

            [cell.contentView addSubview:imageView2];

        }

        if ([sentence count]>2) {

            [imageViewArray insertObject:imageView3 atIndex:2];

            [cell.contentView addSubview:imageView3];
        }

        if ([sentence count]>3) {

            [imageViewArray insertObject:imageView4 atIndex:3];

            [cell.contentView addSubview:imageView4];
        }
        if ([sentence count]>4) {
            [imageViewArray insertObject:imageView5 atIndex:4];

            [cell.contentView addSubview:imageView5];
        }
        if ([sentence count]>5) {
            [imageViewArray insertObject:imageView6 atIndex:5];

            [cell.contentView addSubview:imageView6];
        }
        if ([sentence count]>6) {
            [imageViewArray insertObject:imageView7 atIndex:6];

            [cell.contentView addSubview:imageView7];
        }
        if ([sentence count]>7) {
            [imageViewArray insertObject:imageView8 atIndex:7];

            [cell.contentView addSubview:imageView8];
        }
        if ([sentence count]>8) {
            [imageViewArray insertObject:imageView9 atIndex:8];

            [cell.contentView addSubview:imageView9];
        }
        if ([sentence count]>9) {
            [imageViewArray insertObject:imageView10 atIndex:9];

            [cell.contentView addSubview:imageView10];
        }
        if ([sentence count]>10) {
            [imageViewArray insertObject:imageView11 atIndex:10];

            [cell.contentView addSubview:imageView11];
        }
        if ([sentence count]>11 || ([sentence count]==12)) {
            [imageViewArray insertObject:imageView12 atIndex:11];

            [cell.contentView addSubview:imageView12];
        }

    }

        if ([sentence count]!=0) 
{


    int photosInRow;

        if ( (indexPath.row < 

[tableView numberOfRowsInSection:indexPath.section] - 1) || ([sentence count] % 4 == 0) ) {

        photosInRow = 4;

    } else {

        photosInRow = [sentence count] % 4;
    }


    for ( int i = 1; i <=photosInRow ; i++ ){

        imageView = (UIImageView *)[cell.contentView viewWithTag:j];

        [self setImage1:imageView];
        }

    }
        return cell;


}

2 个答案:

答案 0 :(得分:1)

我认为这里最大的问题是你的imageViews的内存泄漏。每个单元格创建12个imageView,其中没有一个被释放。您似乎只应创建所需的imageViews(以提高速度),并正确释放它们(用于内存管理)。

开始重写此代码的一种方法是:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *AutoCompleteRowIdentifier = @"AutoCompleteRowIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];

    if (!cell) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier] autorelease];

        for (int i=0; i <= [sentence count]; ++i) {
            UIImageView * imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(25+90*(i%4), 4, 70, 80)] autorelease];
            imageView.tag = i+1;
            [cell.contentView addSubview:imageView];
            [imageView release];
        }
    }
    return cell;
}

我不明白为什么要反复设置imageViewArrayimageView1。另外,这一行:

   imageView = (UIImageView *)[cell.contentView viewWithTag:j];

没有意义,因为j未定义。

答案 1 :(得分:0)

  

在使用断点时,我发现索引路径为methid的行的单元格再次被调用,我的数组再次尝试重新加载数据。

没错。如果您不理解,则无法理解表视图的工作原理。细胞是按需创建并重复使用的;当用户滚动时,可以对表的不同行反复使用相同的单元格。这意味着您必须事先准备所有数据 ,并根据需要为所请求的任何部分/行提取。

换句话说,MVC:model-view-controller。您的模型就是您的数据。 tableView:cellForRowAtIndexPath:是观点;你不能在此期间修改模型,因为它的调用时间和频率完全取决于视图的需要。

另一件事是你不应该制作一个单独的图像视图数组。图像视图是视图。如果要存储某些内容列表,请存储图像名称列表或其他内容。

相关问题