我可以向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;
}
答案 0 :(得分:4)
问题在于这一行:
NSString* temp = [biggerDateA objectAtIndex:indexPath.row];
即使你声明这是NSString
,重要的是它是。它是什么,是NSDate
。
你需要它是NSString
,但是你必须明确地转换它,可能是通过NSDateFormatter
传递。
答案 1 :(得分: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;
}