开发者

App crashing when scrolling table view

开发者 https://www.devze.com 2023-03-15 12:50 出处:网络
In my app i have a table view displaying images view ,,4 image view in one row .My issue is that after loading the table view with some data then if i try to scroll the table view then my app is crash

In my app i have a table view displaying images view ,,4 image view in one row .My issue is that after loading the table view with some data then if i try to scroll the table view then my app is crashing.On using the breakpoint i found that cell for row at index path methid is called again and my array tries to reload data again.Please help as its getting on my head now,i am posting my code here:--

- (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];

        UII开发者_Python百科mageView * 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;


}


I believe the biggest problem here is the memory leaks for you imageViews. Each cell creates 12 imageViews, none of which is released. It seems that you should only create the imageViews you need (for speed), and release them correctly (for memory management).

One way to begin to rewrite this code, is the following:

- (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;
}

I don't understand why you are setting imageViewArray or imageView1 repeatedly. Also, this line:

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

makes no sense as j is not defined.


On using the breakpoint i found that cell for row at index path methid is called again and my array tries to reload data again.

That's right. If you don't understand that, you don't understand how a table view works. The cells are created on demand and reused; the same cell might be reused again and again for different rows of the table as the user scrolls. That means that you must prepare all the data beforehand, and simply fetch it on demand for any section/row requested.

To put it another way, MVC: model-view-controller. Your model is your data. tableView:cellForRowAtIndexPath: is view; you must not modify the model during this, because when and how often it is called depends entirely on the needs of the view.

Another thing is that you should not be making a separate array of image views. An image view is view. If you want to store a list of something, store a list of the names of the images or something.

0

精彩评论

暂无评论...
验证码 换一张
取 消