2013-11-21 186 views
1

在我的項目中,我使用Parse.com來檢索表視圖中的數據。如何在TableView中顯示兩個自定義單元格?

我泰伯維有一個名爲FFCustomCell1自定義單元格,其中內有2個標籤:

首先標籤包含我的應用程序 第二個標籤的第一個和最後一個名稱包含報名到我的應用程序的日期。

現在我的問題是,我的應用程序的用戶並不總是把他的照片之間他的數據,然後我需要創建第二個自定義單元格包含FFCustomCell1完全相同的數據添加ImageView的照片用戶。

總而言之,我需要的表格視圖顯示了細胞FFCustomCell1如果沒有圖片或時間的用戶,但如果用戶輸入他的照片,我需要的表格視圖顯示FFCustomCell2

現在根據代碼,我認爲你認爲什麼是實現我的目標的最佳方式?

- (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; 

    } 
+0

我剛編輯我的代碼插入定製的第二小區。當我跑我的應用程序的第二個單元格(CellaIMG)不顯示! 你能告訴我我錯了嗎? – kAiN

回答

1

我不能評論你的問題,所以我寫這個作爲答案。以下是我可能在你的情況下做的事情。

  1. 檢查用戶是否有個人資料圖像(即)他有一個有效的圖像網址。
  2. 如果是的話,我會基本上使用它的標識符實例化第二個自定義單元格。
  3. 更新第二個自定義單元格中的所有信息並將圖像添加到UIImageView。
  4. 回報細胞
+0

它只是我缺乏如何實現第二個單元格的實際示例......顯然忽略了新文件的所有創建自定義單元格...在cellForRowAtIndexPath中部署我想...有一個教程或一些指南我可以按照更好地理解?例如 – kAiN

+0

說我必須爲奇數索引和偶數索引顯示兩個不同的單元格。 tableviewcell1奇數索引和tableviewcell2爲偶數索引 - (的UITableViewCell *)的tableView:(UITableView的*)的tableView的cellForRowAtIndexPath:(NSIndexPath *)indexPath { 甚至 如果(indexPath.row%2 == 0){ UITableViewCell1 * cell = [tableView dequeueReusableCellWithIdentifier:@「evencell」]; //定製細胞 } 否則{ UITableViewCell2 *細胞= [的tableView dequeueReusableCellWithIdentifier:@ 「oddcell」]; //定製單元格 } return cell; } – Roshan

+0

http://stackoverflow.com/questions/14303832/uitableview-with-two-custom-cell-multiple-identifier – Roshan

相關問題