2010-10-30 3 views
-1

J'ai une tableview. J'ajoute de l'image aux cellules lorsqu'on clique sur la cellule dans la méthode didselectrow en utilisant cell.conteview addsubview. mais le problème est que si je clique sur la 1ère cellule, elle change l'image et quand je clique sur une autre image de la cellule apparaîtra mais l'ancienne image n'est pas retirée de la cellule précédente. Cela se produit pour toutes les cellules de la vue tableau si une cellule est cliquée. i utilisé le code comme suitcomment changer l'image lorsque l'on clique sur tableview cellule iphone

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    Practices *bPractices = [topics objectAtIndex:indexPath.row]; 
    UIImageView *clickView; 
    //[cell.contentView removeFromSuperview]; 
    clickView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 315, 82)]; 
    clickView.image = [UIImage imageNamed:@"list_bg_hover.png"]; 
    [cell.contentView addSubview:clickView]; 
    [clickView release]; 

    UILabel *labelText= [[UILabel alloc]initWithFrame:CGRectMake(90, 30, 320, 20)]; 
    labelText.text=bPractices.practices_title; 
    labelText.backgroundColor=[UIColor clearColor]; 
    [cell.contentView addSubview:labelText]; 

} 

pls me aident comment résoudre ce problème

Merci à l'avance

+0

est-ce que vous réutilisez des cellules? Peut-être votre problème ... publiez votre cellule pour la ligne à la méthode de chemin d'index – Daniel

Répondre

0

je faisais quelque chose similaire, mais pas avec des images mais des boutons. Si une cellule n'a pas encore été sélectionnée et qu'elle reçoit des onglets, la taille est modifiée et un certain nombre de boutons individuels sont ajoutés. Si une autre cellule a été sélectionnée, celle-ci se ferme.

code de la forme la UITableViewController

Interface:

int openedCellIndex;//<-what cell is selected 
int buttonCounter; 

mise en œuvre:

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    if (indexPath.row != openedCellIndex) 
    { 
     static NSString *CellIdentifier = @"Cell"; 

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) { 
      cell = [[[TRTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
              reuseIdentifier:CellIdentifier] 
        autorelease]; 
      //cell.frame = CGRectMake (0,0, 320, 100); 
     } 

     id <P_E_P1Article> article = [self.fetchedResultsController objectAtIndexPath:indexPath]; 

     cell.textLabel.text = article.name; 

     if (self.cellBackgroundColor) 
      cell.backgroundColor = self.cellBackgroundColor; 

      return cell; 
    } 
    else { 

     //get article 
     id <P_E_P1Article> article = [self.fetchedResultsController objectAtIndexPath:indexPath]; 

     //number of buttons 
     int buttons = [article.specification count]; 
     int height = 50 * ceil(buttons/2) + 50; 

     //construct special cell 
     UITableViewCell *cell = [[[TRTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                 reuseIdentifier:@"CellBig"] 
           autorelease]; 
     cell.frame = CGRectMake (0,0, 320, 150); 
     cell.textLabel.text = @""; 

     //add label 
     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 280, 30)]; 
     label.font = [UIFont boldSystemFontOfSize:17]; 
     label.text = article.name; 
     label.backgroundColor = [UIColor clearColor]; 
     [cell addSubview:label]; 
     [label release]; 

     if (buttonMapper == nil) 
      self.buttonMapper = [NSMutableDictionary dictionaryWithCapacity:10]; 
      //NSLog (@" bla: %@", article.isFreePrice); 

      //see if we have a free prized article 

      //create the buttons 
      NSEnumerator *enumerator = [article.specification objectEnumerator]; 
      id <P_E_P1ArticleSpecification> spec; 
     int count = 0; 

     NSArray *specs = 
     [self sortedArticleSpecifications:article]; 

     for (spec in specs) { 

      //see which row and col the button is in 
      int row = floor (count/2); 
      int col = count % 2; 

      //define button position 
      int left = 20 + col * 145; 
      int top = 45 + row * 50; 

      //create button 
      UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
      button.frame = CGRectMake(left, top, 135, 40); 
      [cell addSubview:button]; 
      [button setTitleColor:[UIColor blackColor] forState:0]; 

      [button addTarget:self 
         action:@selector(buttonTapped:) 
      forControlEvents:UIControlEventTouchUpInside]; 


      //remember which article the button is attached to 
      buttonCounter++; 

      button.tag = buttonCounter; 

      [buttonMapper setValue:spec forKey:[NSString stringWithFormat:@"bla%d",buttonCounter]]; 

      count++; 
     } 


     if (self.cellBackgroundColor) 
      cell.backgroundColor = self.cellBackgroundColor; 

      return cell; 
    } 

} 


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (openedCellIndex == indexPath.row) 
    { 
     id <P_E_P1Article> article = [self.fetchedResultsController objectAtIndexPath:indexPath]; 
     int count = [article.specification count]; 

     int height = 50 * ceil(count/2.) + 50; 

     return height; 
    } 

    return 50; 
} 


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

    [self openRow:indexPath.row]; 
    [self.searchBar resignFirstResponder]; 


} 

- (void) openRow:(int) index 
{ 
    if (index == openedCellIndex) return; 

    int oldIndex = openedCellIndex; 
    openedCellIndex = index; 


    NSUInteger indexArr[] = {0, oldIndex}; 
    NSIndexPath *oldPath = [NSIndexPath indexPathWithIndexes:indexArr length:2]; 

    NSUInteger indexArr2[] = {0, openedCellIndex}; 
    NSIndexPath *newPath = [NSIndexPath indexPathWithIndexes:indexArr2 length:2]; 


    //update view 
    [(UITableView *)self.view beginUpdates];  
    if (oldIndex >= 0) 
     [(UITableView *)self.view reloadRowsAtIndexPaths:[NSArray arrayWithObject:oldPath] 
             withRowAnimation:UITableViewRowAnimationFade]; 

    if (openedCellIndex >=0) 
     [(UITableView *)self.view reloadRowsAtIndexPaths:[NSArray arrayWithObject:newPath] 
             withRowAnimation:UITableViewRowAnimationFade]; 
    [(UITableView *)self.view endUpdates]; 
} 
0

Vous pouvez également sous-classe UITableViewCell et gérer la sélection/désélectionner processus dans la méthode setSelected. Utilisez ensuite votre cellule personnalisée comme type pour la cellule prototype au lieu de la valeur par défaut.

Questions connexes