2012-05-09 10 views
1

TableViewのサブタイトルに日付と時刻を記録しようとしています。私はテキストフィールドの場合と同じようにサブタイトル用の配列を使用しましたが、サブタイトルでは機能しませんでした。私は何か間違っているのですか? *が付いた字幕のために追加したコード。TableViewサブタイトルに現在の日付を保存する

-(IBAction)save{ 

    *NSDate *now = [NSDate date]; 
    *NSLog(@"now: %@", now); // now: 2012-02-28 09:57:49 +0000 
    *NSString *strDate = [[NSString alloc] initWithFormat:@"%@",now]; 


    NSUserDefaults *add1 = [NSUserDefaults standardUserDefaults]; 
    NSMutableArray *myList = [[[NSUserDefaults standardUserDefaults] valueForKey:@"myTxtList"] mutableCopy]; 
    *NSMutableArray *myDate = [[[NSUserDefaults standardUserDefaults] valueForKey:@"myDateList"] mutableCopy]; 

    [myList addObject:txt1.text]; 
    *[myDate addObject:strDate]; 

    [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithArray:myList] forKey:@"myTxtList"]; 
    *[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithArray:myDate] forKey:@"myDateList"]; 

    [add1 synchronize]; 
    self.dataArray = myList; 
    *self.dateArray = myDate; 
    [self.tbl1 reloadData]; 

} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.dataArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"myTxtList"]; 
    *self.dateArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"myDateList"]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [dataArray count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *string = [dataArray objectAtIndex:indexPath.row]; 
    *NSString *dateString = [dateArray objectAtIndex:indexPath.row]; 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

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

    cell.textLabel.text = string; 
    *cell.detailTextLabel.text = dateString; 


    return cell; 
} 

答えて

2

これをNSDateとして保存してからNSStringとして読み込みます。

また、myListとmyDateの可変コピーをリリースしていないため、ARCを使用しているとします。あなたがARCにいない場合は、これらを解放する必要があります。そうしないと、リークします。

+0

ありがとうございます。コードを編集しましたが、文字列として変更しましたが、まだ動作していません。 'myList'は' myDate'なしで動作しています。通常は私はiOS5を使用していますので、ARCを使用したくありません。どうすれば問題を解決できますか?ジョエルにもう一度感謝します。 –

+1

cellForRowAtIndexPathメソッドでは、dateArrayの数、indexPath.rowを記録してから、配列全体をループし、配列の各エントリを記録します。次に、配列の内容が正しいかどうかを読み取ることができます。問題が配列そのものであるのか、字幕を設定しようとしているのかを判断する必要があります。問題が見つかると、それは簡単に解決できます。 – Joel

+0

ジョエルに感謝します。 'numberOfRowsInSection'に' return [dateArray count]; 'を追加した後で動作します。私は 'numberOfRowsInSection'に2つのカウントを追加できないと思っていました。再度、感謝します。 –

関連する問題