桌面单元格问题

问题描述:

我使用以下代码在桌面视图中显示多个图像。一行需要显示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]; 
    } 

} 

点击按钮至极将显示表视图我写了下面的代码

-(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张图像会显示4.如果显示4张图像后显示3张图像,3张图像正在显示,但仍然以第4张图像显示。我怎样才能解决这个问题。请任何人都可以帮助我...

主要问题是,之前显示的图像视图不会从单元中移除。

+0

来自堆栈溢出的任何辉煌的家伙... – Christina

+0

什么是[cell.contentView addSubview:nil];应该做的? – Sascha

表格单元格可以(并在您的代码中)被缓存和重用 - 这意味着每次调用cellForRowAtIndexPath::时,应确保返回的单元格是正确的(具有正确的图像)。在你的情况下,我只需清除所有子视图/图像视图的单元格,并为每次获取重新创建它们。