2016-10-28 42 views
0

我正在使用一種類型的自定義表格視圖單元格,但是當其他數據發佈到我的表格視圖時,我希望它顯示在不同的自定義表格視圖單元格中同一張桌子。在同一個表視圖中使用兩個不同的自定義表格單元格

例如,我在我的表格視圖中創建了一個聊天。但是,當發佈某些細節時,我想要一個單獨的單元設計來顯示這些細節。到目前爲止,請參閱我的代碼。

我的問題:我怎麼能寫,「如果field_swaptime在self.messages是空的,顯示ChatTableViewCell - 如果它包含數據,顯示SwapDetailTableViewCell」?

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

    static NSString *ChatTableIdentifier = @"ChatTableViewCell"; 
    static NSString *ChatTableIdentifier2 = @"SwapDetailTableViewCell"; 

    NSDictionary *data = [self.messages objectAtIndex:indexPath.row]; 


    if ([data objectForKey:@"field_swaptime"] == nil) { 

    NSLog(@"THIS IS DATA %@", data); 


     ChatTableViewCell *cell = (ChatTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifier]; 
     if (cell == nil) 
     { 
      NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ChatTableViewCell" owner:self options:nil]; 
      cell = [nib objectAtIndex:0]; 
     } 



     NSString *userName = [data objectForKey:@"name"]; 
     [cell.sendingUser setText:userName]; 

     NSString *messageBody = [data objectForKey:@"body"]; 
     [cell.messageDisplayed setText:messageBody]; 

     NSString *timeReceived = [data objectForKey:@"published at"]; 
     NSLog(@"Message Received at %@", timeReceived); 
     [cell.timeStamp setText:timeReceived]; 




     return cell; 
    } 

    else { 


     SwapDetailTableViewCell *cell = (SwapDetailTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifier2]; 
     if (cell == nil) 
     { 
      NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SwapDetailTableViewCell" owner:self options:nil]; 
      cell = [nib objectAtIndex:0]; 

     } 

     NSString *Time = [data objectForKey:@"field_swaptime"]; 
     NSLog(@"This is time %@", Time); 
     [cell.startTime setText:Time]; 

     NSString *TimeEnd = [data objectForKey:@"field_endswaptime"]; 
     [cell.endTime setText:TimeEnd]; 


       return cell; 

    } 

} 

回答

0

你基本上看寫你的初始if爲:

NSDictionary *data = [self.messages objectAtIndex:indexPath.row]; 
// 
// if self.messages is nil, then data will be nil and we'll display 
// a ChatTableViewCell as before. If data is valid, but there is no 
// value associated with "field_swaptime", then again, we'll display 
// a ChatTableViewCell. However, if self.messages IS valid and 
// there IS a value associated with "field_swaptime" key, then the 
// if clause fails, and we execute the else and return a 
// SwapDetailTableViewCell. 
// 
if (!data || ![data objectForKey:@"field_swaptime"] } { 
    ChatTableViewCell *cell = (ChatTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifier]; 
    if (cell == nil) 
    { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ChatTableViewCell" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 
    } 

    NSString *userName = [data objectForKey:@"name"]; 
    [cell.sendingUser setText:userName]; 

    NSString *messageBody = [data objectForKey:@"body"]; 
    [cell.messageDisplayed setText:messageBody]; 

    NSString *timeReceived = [data objectForKey:@"published at"]; 
    NSLog(@"Message Received at %@", timeReceived); 
    [cell.timeStamp setText:timeReceived]; 


    return cell; 
} 
else { 
    SwapDetailTableViewCell *cell = (SwapDetailTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifier2]; 
    if (cell == nil) 
    { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SwapDetailTableViewCell" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 

    } 

    NSString *Time = [data objectForKey:@"field_swaptime"]; 
    [cell.startTime setText:Time]; 

    NSString *TimeEnd = [data objectForKey:@"field_endswaptime"]; 
    [cell.endTime setText:TimeEnd]; 

    return cell; 
} 

獎勵:如果self.messages是有效的,data是有效的,那麼你已經有了data,不需要在多個電話到[self.messages objectAtIndex:indexPath.row]。 (你應該self.messages.count一個邊界的完整性檢查對indexPath.row調用objectAtIndex:indexPath.row之前,但防守編碼部分是由你來放在一起)

目標C是非常漂亮的,調用無對象的方法,只是返回零。因此,忽略self.messages是否有效並僅檢索其中包含的數據是完全有效的。如果它有效(並且索引位於數組的範圍內),那麼將返回一個有效的NSDictionary,您可以在整個if/else子句中使用。

+0

好的1 - 你搖滾發佈這個答案。哈哈。 2)你能解釋一下這一行是什麼意思:if(!data ||![data objectForKey:@「field_swaptime」]){例如是 - 「如果self.messages中的field_swaptime是,則顯示ChatTableViewCell」?我實現了你的代碼,它似乎甚至在數據發佈到field_swaptime之後使用ChatTableViewCell?如果數據發佈時沒有使用field_swaptime,那麼該表應該添加ChatTableViewCell,並且如果field_swaptime存在,則使用SwapDetailTableViewCell(因此,我應該在我的表中看到兩個diff單元格設計)? –

+0

E.g.我不希望整個表格使用同一單元格 - 我應該有一個表格顯示兩種不同的單元格設計,不是嗎? –

+0

是的,正確的。 if是說「如果沒有數據字典(因爲self.messages爲零),或沒有與」field_swaptime「鍵相關的值,則顯示一個ChatTableViewCell,否則顯示一個SwapDetailTableViewCell」 – gschandler

0

您獲得的單元格的類型取決於您註冊的與標識符對應的類型。所以,當你正在設置表(比如,在viewDidLoad)...

UINib *nib = [UINib nibWithNibName:@"ChatTableViewCell" bundle:nil]; // bundle:nil means main bundle 
[self.tableView registerNib:nib forCellReuseIdentifier:@"ChatTableIdentifier"]; 

UINib *nib2 = [UINib nibWithNibName:@"OtherTableViewCell" bundle:nil]; 
[self.tableView registerNib:nib2 forCellReuseIdentifier:@"OtherIdentifier"]; 

然後,在cellForRowAtIndexPath,使用現代方法出隊,dequeueReusableCellWithIdentifier:forIndexPath:,它永遠不會返回零,總是返回內置一個實例從先前註冊的筆尖......

// generically 
NSString *identifier = (/*some condition*/)? @"ChatTableIdentifier" : @"OtherIdentifier"; 

// or alternatively, given the condition in your comment... 
NSString *identifier = (!data[@"field_swaptime"])? @"ChatTableIdentifier" : @"OtherIdentifier"; 


UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath]; 

// now cell is one of your two custom classes, built from their respective nibs. 

基於相同的條件下,你可以投cell要麼善良,並配置它...

if (/*same condition as identifier*/) { 
    ChatTableViewCell *chatCell = (ChatTableViewCell *)cell; 
    // do your config for this type here 
} else { 
    OtherTableViewCell *otherTypeCell = (OtherTableViewCell *)cell; 
    // do your config for this other type here 
} 
return cell; 
+0

這一行:NSString * identifier =(/ * some condition * /)? @「ChatTableIdentifier」:@「OtherIdentifier」;給我悲傷嗎?例如。我的條件是:if(!data ||![data objectForKey:@「field_swaptime」]),但是把它放在聲明的行中表示它仍然期待某種表達式? –

+0

我認爲這只是三元運算符語法,讓你感到沮喪。請參閱編輯,用'//一般地'替換註釋爲''或者......' – danh

+0

實現上述 - 它似乎應該工作,但我仍然只獲得一種單元類型(即使field_swaptime不爲空)! –

相關問題