2011-06-26 11 views
-1

これらのコードを使用して、複数の画像をテーブルビューに表示しています。 1つの行がテーブルビューが表示されます、私は4枚の画像を表示したとき、それが4の場合を表示します。このコードでは以下のコードテーブルビューセルの問題

-(IBAction)setImageOnTableView 
{ 
    NSLog(@"entered setImageOnTableView"); 
    j=1; 

    tableView.hidden=NO; 

    //Breaking the sentence into words and then placing it in an array 
    wordsInSentence=[[youSaid.text uppercaseString] componentsSeparatedByString:@" "]; 
    [wordsInSentence retain]; 
    [youSaid resignFirstResponder]; 

    [tableView reloadData]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = nil; 
    NSLog(@"entered cellForRowAtIndexPath"); 
    //j=1; 
    static NSString *AutoCompleteRowIdentifier = @"AutoCompleteRowIdentifier"; 
    cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier] autorelease]; 

     [cell.contentView addSubview:nil]; 

     for (int i=0; i <= [wordsInSentence count]; ++i) { 
      UIImageView *imageView1 = [[[UIImageView alloc] initWithFrame:CGRectMake(30+90*(i%4), 20, 70, 100)] autorelease] ; 
      imageView1.tag = i+1; 
      [imageViewArray insertObject:imageView1 atIndex:i]; 
      [cell.contentView addSubview:imageView1]; 
      //[imageView1 release]; 
      }  
    } 

    int photosInRow; 
    if ((indexPath.row < [tableView numberOfRowsInSection:indexPath.section] - 1) || ([wordsInSentence count] % 4 == 0)) { 
     photosInRow = 4; 
    } else { 
     photosInRow = [wordsInSentence count] % 4; 
    } 

    for (int i = 1; i <=photosInRow ; i++){    
     imageView = (UIImageView *)[cell.contentView viewWithTag:j]; 
     [self **showImage**:imageView]; 

    } 
    return cell; 
} 


//method used to show the images on the table view by checking the category 
//and images in the category 
-(void)**showImage**:(UIImageView *)imageView 
{ 
    NSLog(@"entered showImage"); 

//this method will check if more than 1 image is present in the category  
    if([self searchImagesInSameCategory:[wordsInSentence objectAtIndex:j-1]]>1) 
    { 
     UIButton *descriptiveButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
     [descriptiveButton setFrame:CGRectMake(50, 0, 30, 30)]; 
     [imageView setUserInteractionEnabled:TRUE]; 

     [descriptiveButton addTarget:self action:@selector(showPopOver1:) forControlEvents:UIControlEventTouchUpInside]; 
     descriptiveButton.tag=j; 
     [imageView addSubview:descriptiveButton]; 
     globalString=[wordsInSentence objectAtIndex:j-1]; 
    } 
} 

を書いたウィッヒをクリックしてボタンで4枚の画像のみ

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section { 

    //Resize auto complete table based on how many elements will be displayed in the table 
    return (int)ceil([wordsInSentence count]/4.0); 
} 


-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"entered forRowAtIndexPath"); 
    if ((indexPath.row % 2) == 0) { 

     cell.backgroundColor = [UIColor whiteColor]; 

    } 
    else { 

     cell.backgroundColor = [UIColor whiteColor]; 
    } 

} 

を表示する必要があります4枚の画像を表示した後に3枚の画像を表示したが、3枚の画像が表示されているが、まだ4枚目の画像がテーブルビューで表示されている。どうしたらいいですか?誰でも助けてください...

主な問題は、前に表示された画像ビューがセルから削除されていないことです。

+0

スタックオーバーフローの華麗な男... – Christina

+0

何[cell.contentView addSubview:nil];やるべきことは? – Sascha

答えて

3

テーブルセルをキャッシュして再利用することができます。つまり、cellForRowAtIndexPath::を呼び出すたびに、返すセルが正しい(正しいイメージがある)ことを確認する必要があります。あなたの場合、私は単にすべてのサブビュー/画像ビューのセルをクリアし、フェッチごとに再作成します。

関連する問題