无法在detailTextLabel中显示NSString

时间:2013-05-16 18:51:18

标签: ios objective-c uitableview nsstring

我可以向UITableView添加文字,但当我尝试将detailTextLabel添加到某个曾经是NSDate的字符串时,该应用会崩溃并说:

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

我可以将常规虚拟NSString添加到detailTextLabel,因此我无法得到它。有什么帮助吗?

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

2 个答案:

答案 0 :(得分:4)

问题在于这一行:

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

即使声明这是NSString,重要的是它。它是什么,是NSDate

你需要它是NSString,但是你必须明确地转换它,可能是通过NSDateFormatter传递。

答案 1 :(得分:2)

尝试使用以下代码转换为NSDateNSString

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