1

In my project I'm using Parse.com to retrieve the data in a table view.

My Tableview has a custom cell called FFCustomCell1 where inside there are 2 label:

First label contains first and last name of my app Second Label contains the date of enrollment to my app.

Now my problem is that the user of my app does not always put his photograph among his data then I would need to create a second custom cell that contains the exact same data of the FFCustomCell1 adding a ImageView with the photo of the user.

In a nutshell I need the table view shows the cell FFCustomCell1 if there is no picture or the time the user but if the user has entered his picture I need the table view shows the FFCustomCell2

Now according to the code that I have here in your opinion what is the best way to achieve my goal?

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [ArrayforPost count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *IdentificazioneCella = @"CellPost";
    static NSString *IdentificazioneCellaIMG = @"CellIMG";

    FFCustomCellTimelineSocial *Cella = nil;
    FFCustomCellWithImage *CellaIMG = nil;

    // FIRST CUSTOM CELL

        Cella = (FFCustomCellTimelineSocial * )[self.FFTableView dequeueReusableCellWithIdentifier:IdentificazioneCella];

    Cella.backgroundCell.layer.cornerRadius = 2.0f;
    Cella.backgroundCell.layer.borderColor = [UIColor colorWithRed:(219/255.0) green:(219/255.0) blue:(219/255.0) alpha:(1)].CGColor;
    Cella.backgroundCell.layer.borderWidth = 1.0f;
    Cella.backgroundCell.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    Cella.BackgroundText.layer.cornerRadius = 2.0f;
    Cella.BackgroundText.layer.borderColor = [UIColor colorWithRed:(219/255.0) green:(219/255.0) blue:(219/255.0) alpha:(1)].CGColor;
    Cella.BackgroundText.layer.borderWidth = 0.0f;
    Cella.BackgroundText.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    Cella.TimeBackground.layer.cornerRadius = 2.0f;
    Cella.TimeBackground.layer.borderColor = [UIColor colorWithRed:(219/255.0) green:(219/255.0) blue:(219/255.0) alpha:(1)].CGColor;
    Cella.TimeBackground.layer.borderWidth = 1.0f;
    Cella.ViewTestataCell.layer.cornerRadius = 2.0f;
    Cella.ViewTestataCell.layer.borderColor = [UIColor colorWithRed:(219/255.0) green:(219/255.0) blue:(219/255.0) alpha:(1)].CGColor;
    Cella.ViewTestataCell.layer.borderWidth = 1.0f;
    Cella.FFImmagineUtente.layer.masksToBounds = YES;
    Cella.FFImmagineUtente.layer.cornerRadius = 20.0f;
    Cella.FFImmagineUtente.contentMode = UIViewContentModeScaleAspectFill;


    PFObject *ObjectPost = [ArrayforPost objectAtIndex:indexPath.row];
    Cella.FFTestoUtenteLabel.font = [UIFont fontWithName:@"Helvetica" size:14.0f];
    NSString *text = [ObjectPost objectForKey:@"Testo"];
    Cella.FFTestoUtenteLabel.text = text;
    [Cella.FFTestoUtenteLabel setLineBreakMode:NSLineBreakByTruncatingTail];

    NSDate *currDate = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"dd MMM"];
    NSString *dateString = [dateFormatter stringFromDate:currDate];
    Cella.DataCorrente.text = dateString;

    NSString *NomeUser = [[ObjectPost objectForKey:FF_POST_UTENTE] objectForKey:FF_USER_NOMECOGNOME];
    Cella.FFNomeUtenteLabel.text = NomeUser;

    NSString *ImmagineUtente = [[ObjectPost objectForKey:FF_POST_UTENTE] objectForKey:FF_USER_FOTOPROFILO];
    Cella.FFImmagineUtente.file = (PFFile *)ImmagineUtente;
    [Cella.FFImmagineUtente loadInBackground];

    if (CellaSelezionata == indexPath.row) {
        CGFloat AltezzaLabel = [self valoreAltezzaCella: indexPath.row];
        Cella.FFTestoUtenteLabel.frame = CGRectMake(Cella.FFTestoUtenteLabel.frame.origin.x, Cella.FFTestoUtenteLabel.frame.origin.y, Cella.FFTestoUtenteLabel.frame.size.width, AltezzaLabel);  }

    else {

        Cella.FFTestoUtenteLabel.frame = CGRectMake(Cella.FFTestoUtenteLabel.frame.origin.x, Cella.FFTestoUtenteLabel.frame.origin.y, Cella.FFTestoUtenteLabel.frame.size.width, 55);
    }

    PFObject *rowObject = [ArrayforPost objectAtIndex:indexPath.row];
    if([[rowObject objectForKey:FF_POST_FLASH_POST_BOOLEANVALUE ] boolValue]) {
        Cella.FlashPostImg.image = [UIImage imageNamed:@"FFNotificaFlash"];
    }

    else {
        Cella.FlashPostImg.image =  [UIImage imageNamed:@" "]; }

    return Cella;


        // SECOND CUSTOM CELL

    PFObject *Immagine = [self.ArrayForPhoto objectAtIndex:indexPath.row];
    if ([Immagine objectForKey:FF_POST_IMMAGINE]) {
        CellaIMG = (FFCustomCellWithImage * )[self.FFTableView dequeueReusableCellWithIdentifier:IdentificazioneCellaIMG];

        NSString *FotoPostSocial = [Immagine objectForKey:FF_POST_IMMAGINE];
        CellaIMG.FotoPost.file = (PFFile *)FotoPostSocial;
        [ CellaIMG.FotoPost loadInBackground];

    }

    return CellaIMG;

    }
kAiN
  • 2,559
  • 1
  • 26
  • 54
  • I just edited my code by inserting a second cell customized .. When do I run my app the second cell (CellaIMG) is not displayed! Can you tell me where I'm wrong? – kAiN Nov 22 '13 at 13:35

1 Answers1

1

I can't comment to your question so i am writing this as an answer. The following is what i might do in your situation.

  1. check if the user has a profile image (i.e.) he has a valid image url.
  2. if yes, i would instantiate the second custom cell, by basically using its identifier.
  3. update all the info in the second custom cell and add image to the UIImageView.
  4. return that cell
Roshan
  • 521
  • 4
  • 16
  • It 'just that I lack the practical example of how to implement a second cell ... obviously leaving out all the creations of new files custom cell ... deployment in cellForRowAtIndexPath I miss ... there is' a tutorial or some guide that I can follow to better understand? – kAiN Nov 21 '13 at 18:00
  • say for example i have to show two different cells for odd index and even index. tableviewcell1 for odd index and tableviewcell2 for even index - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ even if(indexPath.row % 2 == 0){ UITableViewCell1* cell = [tableView dequeueReusableCellWithIdentifier:@"evencell"]; //customize the cell } else{ UITableViewCell2* cell = [tableView dequeueReusableCellWithIdentifier:@"oddcell"]; //customize the cell } return cell; } – Roshan Nov 21 '13 at 18:25
  • http://stackoverflow.com/questions/14303832/uitableview-with-two-custom-cell-multiple-identifier – Roshan Nov 21 '13 at 18:49
  • Thanks for the indication! I was reading the link that you have shown me and I noticed that work with TR I am doing everything from storiboard without using a custom cell xib for ... also because I read that apple does not want more 'xib in the projects so how could I replace the code where recalls the xib with the classic FFCustomCell * cell = (FFCustomCell *) [self.FFTableView dequeueReusableCellWithIdentifier: @ "CellPost"]; ? – kAiN Nov 21 '13 at 20:50