2011-06-20 7 views
0

私のアプリでは、配列を使用して画像ビュー内の画像を表示しています。この配列は、テキストボックスに入力したコンポーネントをスペースで区切って格納するために使用します。例:i少年少女は、配列が他のように、1つのオブジェクトとガールとして少年を格納し、each.followingの画像を検索します入ったことは私のコードです: -xcodeのEXC_BAD_ACCESS

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

私は他の方法で私のアレイにアクセスしていたとき、私はEXC_BAD_ACCESSを取得しています何がcoludのためにこれの理由です。助けてください。

おかげで、 クリスティ

答えて

1

保持....

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

はい、私はahvが.h filでこれを保持していることを意味します。 – Christina

+0

componentsSeparatedByStringはオートレリースされたオブジェクトを返すので、保持しないと解放されます。あなたは後でBOOOOOOMにアクセスしよう! –

+0

ありがとうございます.great – Christina

1

sentenceretain属性を持つクラスのプロパティである場合は、ドット表記の構文を使用します、それは
self.sentence = [youSaid.text componentsSeparatedByString:@" "];
ですsentenceオブジェクトを適切に保持する必要があります。 deallocメソッドでsentenceを解放して、メモリリークを回避してください。

関連する問題