2012-10-03 11 views
5

tworzę zwyczaj komórka zawiera UILabel, UIImageView, wykorzystując stały znacznik do UILabel, UIImageView stosując tag dynamiczny, tabela ma 11 komórek, pierwsze 7 komórki poprawnie załadunku, 8, 9, 10, 11 obraz komórki zmienia się, gdy zmieniam odpowiednio 1, 2, 3, 4 komórkę w tabeli, również znaczniki są takie same w komórkach, używam obrazów do zaznaczenia w tabeli, UITapGestureRecognizer użytych do zmiany ImageView w tabeli,dlaczego tag zmieniło w tableview komórek reuseing

Używam tego kodu .....

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

    static NSString *CellIdentifier [email protected]"Cell"; 
    UITableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:CellIdentifier]; 


    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ; 
     cell.selectionStyle=UITableViewCellSelectionStyleGray; 
     cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator; 
     UIImageView *imageview=[[UIImageView alloc]initWithFrame:CGRectMake(5, 12, 20, 20)]; 
     imageview.tag=n; 
     [cell.contentView addSubview:imageview]; 
     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tabimage:)]; 
     imageview.userInteractionEnabled=YES; 
     [imageview addGestureRecognizer:tap]; 
     imageview.image=[UIImage imageNamed:@"img1.jpeg"]; 

     UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(30, 2, 260,26)]; 
     titleLabel.tag=222; 
     titleLabel.backgroundColor=[UIColor clearColor]; 
     [cell.contentView addSubview:titleLabel]; 

     UILabel *dateLabel=[[UILabel alloc]initWithFrame:CGRectMake(30, 31, 260, 13)]; 
     dateLabel.tag=333; 
     dateLabel.font=[UIFont systemFontOfSize:10]; 
     dateLabel.backgroundColor=[UIColor clearColor]; 
     [cell.contentView addSubview:dateLabel]; 
    } 
    UIImageView *imageview1=(UIImageView*)[cell.contentView viewWithTag:n]; 
    if([array containsObject:[NSNumber numberWithInt:imageview1.tag]]) { 
     imageview1.image=[UIImage imageNamed:@"img2.jpeg"]; 
    } else { 
     imageview1.image=[UIImage imageNamed:@"img1.jpeg"]; 
    } 

    UILabel *titlelable=(UILabel*)[cell.contentView viewWithTag:222]; 
    titlelable.text=[task objectAtIndex:indexPath.section]; 
    NSLog(@"%i",indexPath.section); 

    UILabel *dateLabel=(UILabel*)[cell.contentView viewWithTag:333]; 
    dateLabel.text=[date objectAtIndex:indexPath.section]; 
    n++; 
    return cell; 
} 

- (void)tabimage:(id)sender { 
    UIImageView *iv=(UIImageView *)[sender view]; 
    int i=iv.tag; 
    NSLog(@"------------%i",i); 
    if (iv.image==[UIImage imageNamed:@"img1.jpeg"]) { 
     iv.image= [UIImage imageNamed:@"img2.jpeg"]; 
     [array addObject:[NSNumber numberWithInt:i]]; 
    } else { 
     iv.image= [UIImage imageNamed:@"img1.jpeg"]; 
     [array removeObject:[NSNumber numberWithInt:i]]; 
    } 
    } 
+2

.. . i? Jaki masz problem? – Cyrille

Odpowiedz

2

jestem coraz rozwiązanie dla mojej aplikacji,

użyciem dynamiczny identyfikator komórki w moim kodu

NSString *CellIdentifier =[NSString stringWithFormat:@"%i-%i", indexPath.section,indexPath.row]; 
+1

To jest dokładnie moja odpowiedź, dlaczego wtedy nie przyjąłeś mojej. –

4

Jakikolwiek problem widzisz, używanie tego "n" jest najprawdopodobniej problemem. Nie ma żadnego powodu, aby używać zmiennej współdzielonej do oznaczania (i pobierania) widoków w komórkach widoku tabeli.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath jest nazywany wiele razy, może być wywoływany kilka razy dla tej samej komórki, a czasami znajduje komórkę do ponownego użycia, a czasami nie.

Oczywiście nie mam pojęcia, co to jest n i jak go używasz gdzie indziej w swoim kodzie, ale zmieniasz go przy każdym wywołaniu tutaj i nie możesz założyć, kiedy ta metoda jest wywoływana, więc jest bezwartościowa.

Wiesz na pewno, że każda komórka ma jeden obraz z tym znacznikiem, ale jeśli kiedykolwiek będzie dostępna po pierwszym uruchomieniu tej metody, jest całkowicie nieokreślona, ​​więc przy każdym ponownym użyciu otrzymuje się zasadniczo zachowanie losowe (albo świeżą nową komórkę lub ponownie wykorzystaną z nie znanym tagiem).

Zadaj sobie pytanie (a może w SO w innym pytaniu): Co próbujesz osiągnąć tutaj?

+0

Zgadzam się, nie widzę, że 'n' jest ustawione * gdziekolwiek * w kodzie, więc nawet z tym ogromnym blokiem kodu nie możemy całkowicie powiedzieć, co się dzieje. –

8

Należy zmienić identyfikator komórki ze statycznej na dynamiczną, która rozwiąże problem.

Należy zastąpić tę

static NSString *CellIdentifier [email protected]"Cell"; 
UITableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:CellIdentifier]; 

z tym

UITableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"%d %d"], indexPath.row, indexPath.section]; 
+0

działa poprawnie NSString * CellIdentifier = [NSString stringWithFormat: @ "% i-% i", indexPath.sekcja, indexPath.row]; –

1

proponuję podklasy UITableViewCell dodać tam wszystkie elementy niestandardowe, tworzyć ustawiające dla niego. A w cellForRowAtIndexPath po prostu zadzwoń do swoich seterów.

0

Podczas ustawiania znacznika na widok obrazu, jeśli blok (komórka == zero) oznacza, że ​​znacznik zostanie ustawiony podczas tworzenia komórki. W twoim przypadku powstaje 7 komórek po tym, jak komórki zostaną ponownie użyte przez odciśnięcie. W związku z tym od 8 lat będą używane wcześniej utworzone komórki. Dlatego twoje tagi są takie same jak 1,2,3 ... dla 8,9,10 ... komórek.

Cóż, Alex zasugerował standardowy sposób na zrobienie tego. Ale jeśli nie używano jeszcze własnych komórek, można spróbować this-

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

    static NSString *CellIdentifier [email protected]"Cell"; 
    UITableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:CellIdentifier]; 
    UIImageView *imageview = nil;  
    if(cell){ 
     for (UIView *view in cell.contentView.subviews){ 
      if([view isKindOfClass:[UIImageView class]]){ 
        imageview = (UIImageView*)view; 
        break; 
      } 
     } 
    } 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ; 
     cell.selectionStyle=UITableViewCellSelectionStyleGray; 
     cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator; 
     imageview=[[UIImageView alloc]initWithFrame:CGRectMake(5, 12, 20, 20)]; 
     [cell.contentView addSubview:imageview]; 
     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tabimage:)]; 
     imageview.userInteractionEnabled=YES; 
     [imageview addGestureRecognizer:tap]; 
     imageview.image=[UIImage imageNamed:@"img1.jpeg"]; 

     UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(30, 2, 260,26)]; 
     titleLabel.tag=222; 
     titleLabel.backgroundColor=[UIColor clearColor]; 
     [cell.contentView addSubview:titleLabel]; 

     UILabel *dateLabel=[[UILabel alloc]initWithFrame:CGRectMake(30, 31, 260, 13)]; 
     dateLabel.tag=333; 
     dateLabel.font=[UIFont systemFontOfSize:10]; 
     dateLabel.backgroundColor=[UIColor clearColor]; 
     [cell.contentView addSubview:dateLabel]; 
    } 
    imageview.tag=n; 
    if([array containsObject:[NSNumber numberWithInt:imageview.tag]]) { 
     imageview.image=[UIImage imageNamed:@"img2.jpeg"]; 
    } else { 
     imageview.image=[UIImage imageNamed:@"img1.jpeg"]; 
    } 

    UILabel *titlelable=(UILabel*)[cell.contentView viewWithTag:222]; 
    titlelable.text=[task objectAtIndex:indexPath.section]; 
    NSLog(@"%i",indexPath.section); 

    UILabel *dateLabel=(UILabel*)[cell.contentView viewWithTag:333]; 
    dateLabel.text=[date objectAtIndex:indexPath.section]; 
    n++; 
    return cell; 
} 
Powiązane problemy