Zach Lucas
Zach Lucas

Reputation: 1641

Cannot display a NSString in detailTextLabel

I can add text to my UITableView, but when I try to add to detailTextLabel a certain string that used to be a NSDate, the app crashes and says:

'NSInvalidArgumentException', reason: '-[__NSDate isEqualToString:]:
 unrecognized selector sent to instance 0x8383690'

I can add a regular dummy NSString to detailTextLabel, so I don't get it. Any help?

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


    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }


    NSString* temp = [biggerDateA objectAtIndex:indexPath.row];


    cell.textLabel.text = [bigA objectAtIndex:indexPath.row];
    //cell.detailTextLabel.text = [biggerDateA objectAtIndex:indexPath.row];
    //cell.detailTextLabel.text = @"Date Goes here";
    cell.detailTextLabel.text = temp;

    [bigA retain];
    //[biggerDateA retain];
    return cell;
}

Upvotes: 0

Views: 436

Answers (2)

Manish Agrawal
Manish Agrawal

Reputation: 11026

Try using following code by converting to NSDate to NSString.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    NSDate* date = [biggerDateA objectAtIndex:indexPath.row];
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    [format setDateFormat:@"MMM dd, yyyy HH:mm"];
    NSString *temp = [format date];
    cell.textLabel.text = [bigA objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = temp;
    [bigA retain];
    //[biggerDateA retain];
    return cell;
}

Upvotes: 2

matt
matt

Reputation: 535557

The problem is this line:

NSString* temp = [biggerDateA objectAtIndex:indexPath.row];

Even though you declare this is an NSString, what matters is what it is. And what it is, is an NSDate.

You need it to be an NSString, but then you must explicitly convert it, probably by passing through an NSDateFormatter.

Upvotes: 4

Related Questions