2010-12-30 3 views
2

Mon application qui télécharge des messages depuis Internet vers UITableViewCell. Lorsque l'utilisateur appuie sur la cellule, je dois définir cet article comme lu (comme sur Mail.app). Je ne sais pas comment faire ça. J'ajoute l'url (quand l'utilisateur presse la cellule) à la base de données (comme "url | url | url"), ouvre la vue détaillée. Et quand il retourne à UITableView vérifier l'URL existante dans DB. La table est maintenant trop lente! Pouvez-vous m'aider et dire quelle autre méthode je peux utiliser pour faire cela? Comme dans mail.app. J'utilise la cellule et le code personnalisés dans la méthode drawRect. S'il vous plaît, aidez-Cellule lecture/lecture (utilisant drawrect)

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
     { 
      NSString *CellIdentifier = [NSString stringWithFormat:@"%d",indexPath.row]; 
      NSMutableArray *array=[NSMutableArray array]; 
      if([[NSUserDefaults standardUserDefaults] objectForKey:@"readPostS"]!=nil) 
    [array addObjectsFromArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"readPostS"]]; 
      NSUInteger indexOfObject = [array count]>0?[array indexOfObject:[NSNumber numberWithInt:indexPath.row]]:100000; //I add this, because row don't want update using updateRowAtIndexPath 

      QuickCell *cell = (QuickCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
      if (cell == nil || (indexOfObject!=NSNotFound && indexOfObject!=100000)) 
      { 
       cell = [[[QuickCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
       cell.frame = CGRectMake(0.0, 0.0, 320.0, 68.0); 
       cell.accessoryType = UITableViewCellAccessoryNone; 
       if([self.subject count]>0) 
      { 
       [self.allImages addObject:[NSNull null]]; 
       NSString *userNameLJ = [NSString stringWithFormat:@"%@",[self.journalurl objectAtIndex:indexPath.row]]; 
       userNameLJ = [userNameLJ stringByReplacingOccurrencesOfString:@"http://" 
                    withString:@""]; 
       userNameLJ = [userNameLJ stringByReplacingOccurrencesOfString:@".livejournal.com" 
                    withString:@""]; 
       userNameLJ = [userNameLJ stringByReplacingOccurrencesOfString:@"community/" 
                    withString:@""]; 
       NSString *postURL = [NSString stringWithFormat:@"http://m.livejournal.com/read/user/%@/%@",userNameLJ,[self.ditemid objectAtIndex:indexPath.row]]; 


       NSMutableDictionary *dict = [NSMutableDictionary dictionary]; 
       [dict setObject:[self.subject objectAtIndex:indexPath.row] forKey:@"title"]; 
       [dict setObject:[Utilities replaceForCell:[Utilities flattenHTML:[self.entry objectAtIndex:indexPath.row]]] forKey:@"description"]; 

       if([[database executeQuery:@"SELECT * FROM read WHERE posts=?;",postURL] count]>0) 
      { 
       //if this post is read 
       [dict setObject:[NSNumber numberWithBool:NO] forKey:@"highlighted"]; 
      } 
       else { 
        [dict setObject:[NSNumber numberWithBool:YES] forKey:@"highlighted"]; 
       } 


      [dict setObject:[self.journalname objectAtIndex:indexPath.row] forKey:@"nick"]; 

      [cell setContentForCell:dict]; 

       BOOL trueOrFalse = [[self.allImages objectAtIndex:indexPath.row] isKindOfClass:[NSNull class]]?YES:NO; 
      if(trueOrFalse) 
     { 
      if (tableView.dragging == NO && tableView.decelerating == NO) 
      { 
        //if no image cached now, download it 
       if(indexPath.row<6 && self.updating==YES) 
        [self startDownloading:indexPath]; 
       if(self.updating==NO) 
        [self startDownloading:indexPath]; 
      } 

      } 
      else 
      { 
       [cell setImageForCell:[self.allImages objectAtIndex:indexPath.row]]; 
     } 

      } 

       if(indexOfObject!=NSNotFound && indexOfObject!=100000) 
       { 
         //delete this row from userdefaults 
        [array removeObjectAtIndex:indexOfObject]; 
        if([array count]>0) 
         [[NSUserDefaults standardUserDefaults] setObject:array forKey:@"readPostS"]; 
        else [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"readPostS"]; 

       } 
      } 


      return cell; 
     } 

////////////////////////////////////////////////////////////// 
///////-(void)viewWillApear 
////////////////////////////////////////////////////////////// 
    NSMutableArray *readPosts = [NSMutableArray array]; 
       if([[NSUserDefaults standardUserDefaults] objectForKey:@"readPostS"]!=nil) 
       { 
        [readPosts addObjectsFromArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"readPostS"]]; 
        self.read = (NSMutableArray*)[database executeQuery:@"SELECT * FROM read;"]; 
        for(int i=0;i<[readPosts count];i++) 
        { 

        [self.myTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:[[readPosts objectAtIndex:i] intValue] inSection:0]] withRowAnimation:UITableViewRowAnimationNone]; 
        } 
       } 

/////////////////////////////////////////////// 
///////THIS FILE OPENS POSTS FOR READING 
/////////////////////////////////////////////// 
if([[database executeQuery:@"SELECT * FROM read WHERE posts=?;",self.postURL] count]==0) 
    { 
     [database executeNonQuery:@"INSERT INTO read VALUES (?);",self.postURL]; 

     NSMutableArray *defaults = [NSMutableArray array]; 
     if([[NSUserDefaults standardUserDefaults] objectForKey:@"readPostS"]!=nil) 
      defaults = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"readPostS"]]; 
     [defaults addObject:[NSNumber numberWithInt:self.currentIndex]]; 
     [[NSUserDefaults standardUserDefaults] setObject:defaults forKey:@"readPostS"]; 
    } 

Ok. Je pense, mon code est mauvais et vous pouvez comprendre, ce que je fais ici) Réponse sur l'autre question. Comment puis-je mettre à jour les cellules cachées? L'utilisateur ne voit que 6 cellules dans un instant, j'ai besoin de mettre à jour, par exemple, 10 cellules. Comment? Ou, comment recharger la cellule, si elle existe déjà? Disons -

NSString *CellIdentifier = [NSString stringWithFormat:@"%d",indexPath.row]; 
QuickCell *cell = (QuickCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[[QuickCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     cell.frame = CGRectMake(0.0, 0.0, 320.0, 68.0); 
     cell.accessoryType = UITableViewCellAccessoryNone; 


.........} 

maintenant la cellule existe et si j'appelle les données de rechargement rien ne se passe. Et reloadCellAtIndexPath ne fonctionne pas aussi, car la cellule a un identificateur unique et ixists.

COMMENT JE RELOAD CELLULAIRE ET APPEL drawRect ENCORE?))

Répondre

0

Vous pouvez implémenter cette fonctionnalité en ajoutant votre code dans le

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 

//Add the indexPath.row values in an array. 
} 

Ajouter la fonctionnalité personnalisée (comme montrant le titre dans BOLD pour non lus) dans le

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

//Check if the indexPath.row is available in the array 
//If yes then Normalize the fonts 
//Else Keep them BOLD. 

} 
+0

oui. Mais problème ici - l'utilisateur peut lire les messages sans revenir sur la page principale, comme sur Mail.app. Il peut lire, par exemple, 10 messages. Quand il reviendra, seulement une rangée dans la cellule sera non lue. J'ai essayé d'utiliser NSUserDefaults, mais, je pense, Apple a un bug. Un moment. Je montre un code –

+0

Ensuite, vous devez mettre à jour votre tableau lorsque l'utilisateur appuie sur le bouton suivant pour lire l'article suivant, tout comme Mail App. – Aditya

+0

Tout fonctionnant comme un charme, MAIS lorsque l'utilisateur met à jour tous les messages sur l'onglet principal, tous les messages lus maintenant UNREAD et quand il appuie à nouveau sur update evrething allright, puis appuyez à nouveau sur update ....... etc. WTF?)) –

0

à moins que vous faites vraiment un certain dessin, ne met pas en œuvre dans drawRect:. Je ne sais pas que vous êtes un code spécifique, ce qui peut ne pas être correct dans votre cas.

Pour recharger la table entière (toutes cellules), regardez - (void)reloadData de UITableView

Pour recharger une cellule spécifique, regardez UITableView de - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation et l'utiliser pour recharger la cellule en question.