【问题标题】:Cannot display a NSString in detailTextLabel无法详细显示 NSStringTextLabel
【发布时间】:2013-05-16 18:51:18
【问题描述】:

我可以向 UITableView 添加文本,但是当我尝试向 detailTextLabel 添加某个曾经是 NSDate 的字符串时,应用程序崩溃并显示:

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

我可以在detailTextLabel 中添加一个普通的虚拟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];
    }


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

【问题讨论】:

  • “曾经是 NSDate 的字符串”?什么意思?
  • 你确定certain stringstring 类型吗?
  • biggerDateA 持有什么类型的对象?看起来像 NSDate 对象。当然,您不能将 text 属性设置为日期。
  • 您几乎可以肯定是在尝试将 NSDate 分配给标签。

标签: ios objective-c uitableview nsstring


【解决方案1】:

问题出在这一行:

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

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

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

【讨论】:

  • @ZachLucas 请将 matt 的答案或 Mayank 的答案标记为正确。谢谢。
【解决方案2】:

尝试使用以下代码,将NSDate 转换为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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 2016-05-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-05
    • 2011-07-16
    相关资源
    最近更新 更多