Vipin
Vipin

Reputation: 4728

EXC_BAD_ACCESS in xcode

in my app i am using an array to print the images in the image View present in the table view.The array is used to store the components i entered in the text box,each one separated by space e.g i entered Boy Girl then the array will store Boy as one object and Girl as other and then search the images of each.following is my code:-

-(IBAction)Click
{

    //count = [youSaid.text intValue];
    //count=1;
    //NSArray *sub= [youSaid.text componentsSeparatedByString:@" "];
    j=1;
    sentence=[youSaid.text componentsSeparatedByString:@" "];

    //[substring addObject:youSaid.text];
    [tableView reloadData];
}

- (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, 80, 80)] autorelease];
        UIImageView * imageView2 = [[[UIImageView alloc] initWithFrame:CGRectMake(115,4,80, 80)] autorelease];
        UIImageView * imageView3 = [[[UIImageView alloc] initWithFrame:CGRectMake(205,4, 80, 80)] autorelease];
        UIImageView * imageView4 = [[[UIImageView alloc] initWithFrame:CGRectMake(295,4, 80, 80)] autorelease];
        imageView1.tag = j;
        imageView2.tag = j+1;
        imageView3.tag = j+2;
        imageView4.tag = j+3;
        //imageView1.tag=t;
//      t++;
//      imageView2.tag=t;
//      t++;
//      imageView3.tag=t;
//      t++;
//      imageView4.tag=t;
//      t++;

        [cell.contentView addSubview:imageView1];
        [cell.contentView addSubview:imageView2];
        [cell.contentView addSubview:imageView3];
        [cell.contentView addSubview:imageView4];

    }
    //[sentence addObject:@"BLINK"];
    // UIImageView * imageView;
    //for ( int i = 1; i <= j; i++ ) {
        for ( int i = 1; i <= j; i++ ) {
        imageView = (UIImageView *)[cell.contentView viewWithTag:i];

        //button = (UIButton *)[cell.contentView viewWithTag:i];
        imageView.image = nil;

        //[button setImage:nil forState:UIControlStateNormal];
    }


    int photosInRow;
    //NSLog([NSString stringWithFormat:@"%d", [sentence count]]);

    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++ ) 
    for ( int i = 1; i <=[sentence count]; i++ ){
        imageView = (UIImageView *)[cell.contentView viewWithTag:i];

        //button = (UIButton *)[cell.contentView viewWithTag:i];

        [self setImage1:imageView];

        //[self setImage1:button];

        //imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png", i]];

        //imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png", youSaid.text]];

                //imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"1.png"]];

    }



    //cell.textLabel.text = [autoCompleteArray objectAtIndex:indexPath.row];
    return cell;
}

I am getting EXC_BAD_ACCESS when i am accessing my array in other method.What colud be the reason for this.Please help.

Thanks, Christy

Upvotes: 0

Views: 793

Answers (2)

Kongress
Kongress

Reputation: 2254

If sentence is a class property with the retain attribute, use the dot notation syntax, that is
self.sentence = [youSaid.text componentsSeparatedByString:@" "];
to properly retain the sentence object. Make sure you release sentence in your dealloc method to avoid memory leaks.

Upvotes: 1

Simon Lee
Simon Lee

Reputation: 22334

Add a retain....

sentence=[youSaid.text componentsSeparatedByString:@" "];
[sentence retain];

Upvotes: 1

Related Questions