Brittany Brittany - 1 month ago 15
Objective-C Question

TableView not using two different custom table view cell identifiers

I have a Table View that uses two different Table Cell Identifiers (two different custom cells). If field_swaptime is null, I want my Table View to use one cell; if field_swaptime contains data, I want it to use the other. That said, for some reason, my Table View is ONLY using ChatTableCell (and not SwapDetailTableCell, no matter what field_swaptime contains). Does anyone know why this might be?

Here is what 'data' (self.messages) spits out - and even though field_swaptime has data in some cases and is null in another, the same custom cell is used for all results:

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


And here is my code:

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

}

}

Answer

You have several issues when trying to access values from data.

To start, there is no such key as field_swaptime. There is the key swaptime. But once you fix that reference, you need to be aware that some values have an actual date value and some have a "null" value (from NSNull), so your simple check for the existence of the key is insufficient.

Try this:

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

The above code also cleans up lots of other little things such as using modern syntax for accessing array and dictionary values. It also moves the static variables so they are only in the scope they are needed.

Comments