2016-10-28 41 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不为空)! –

相关问题