我有一个表视图,它使用两个不同的表格单元标识符(两个不同的自定义单元格)。如果field_swaptime为null,我希望我的表视图使用一个单元格;如果field_swaptime包含数据,我希望它使用另一个。也就是说,出于某种原因,我的表视图仅使用ChatTableCell(而不是SwapDetailTableCell,无论field_swaptime包含什么)。有谁知道为什么会这样?
以下是数据'吐出(self.messages) - 即使field_swaptime在某些情况下有数据而在另一种情况下为null,同一个自定义单元格用于所有结果:
> THIS IS DATA {
> body = "28-10-2016 18:05\n";
> endswaptime = "28-10-2016 19:05";
> name = Brittany;
> "node_title" = "Title";
> "published at" = "Saturday, October 29, 2016 - 00:05";
> swaptime = "28-10-2016 18:05";
> targetuser = 93;
> uid = 47; } 2016-10-28 21:37:16.196852 [2594:626224] Message Received at Saturday, October 29, 2016 - 00:05 2016-10-28
> 21:37:16.197365 [2594:626224] THIS IS DATA {
> body = "04-11-2016 12:54\n";
> endswaptime = "13-12-2016 21:54";
> name = Brittany;
> "node_title" = "Title!";
> "published at" = "Friday, October 28, 2016 - 23:55";
> swaptime = "04-11-2016 12:54";
> targetuser = 93;
> uid = 47; } 2016-10-28 21:37:16.197688n [2594:626224] Message Received at Friday, October 28, 2016 - 23:55 2016-10-28
> 21:37:16.198268 [2594:626224] THIS IS DATA {
> body = "why";
> endswaptime = "<null>";
> name = Brittany;
> "node_title" = "Re:";
> "published at" = "Friday, October 28, 2016 - 23:50";
> swaptime = "<null>";
> targetuser = 93;
> uid = 47; }
这是我的代码:
的.m
- (void)viewDidLoad {
[super viewDidLoad];
static NSString *ChatTableIdentifier = @"ChatTableViewCell";
static NSString *ChatTableIdentifier2 = @"SwapDetailTableViewCell";
UINib *nib = [UINib nibWithNibName: ChatTableIdentifier bundle:nil];
[self.tableView registerNib:nib forCellReuseIdentifier: ChatTableIdentifier];
UINib *nib2 = [UINib nibWithNibName: ChatTableIdentifier2 bundle:nil];
[self.tableView registerNib:nib2 forCellReuseIdentifier: ChatTableIdentifier2];
}
- (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"]) {
NSLog(@"THIS IS DATA %@", data);
ChatTableViewCell *cell = (ChatTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifier forIndexPath:indexPath];
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 forIndexPath:indexPath];
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 :(得分:0)
尝试访问data
中的值时,您遇到了几个问题。
首先,没有field_swaptime
这样的密钥。有一个键swaptime
。但是一旦你修复了这个引用,你需要知道一些值有一个实际的日期值,有些值有一个“null”值(来自NSNull
),所以你对该键存在的简单检查是不够的。
试试这个:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *data = self.messages[indexPath.row];
NSLog(@"THIS IS DATA %@", data);
id swaptime = data[@"swaptime"];
if ([swaptime isKindOfClass:[NSString class]]) {
// There is a valid "swaptime" value
static NSString *ChatTableIdentifier2 = @"SwapDetailTableViewCell";
SwapDetailTableViewCell *cell = (SwapDetailTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifier2 forIndexPath:indexPath];
NSString *time = data[@"swaptime"];
cell.startTime.text = time;
NSString *timeEnd = data[@"endswaptime"];
cell.endTime.text = timeEnd;
return cell;
} else {
static NSString *ChatTableIdentifier = @"ChatTableViewCell";
ChatTableViewCell *cell = (ChatTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifier forIndexPath:indexPath];
NSString *userName = data[@"name"];
cell.sendingUser.text = userName;
NSString *messageBody = data[@"body"];
cell.messageDisplayed.text = messageBody;
NSString *timeReceived = data[@"published at"];
cell.timeStamp.text = timeReceived;
return cell;
}
}
上面的代码还清理了许多其他小东西,例如使用现代语法来访问数组和字典值。它还会移动static
变量,因此它们只在需要的范围内。