2012-06-25 2 views
0

단추를 클릭 할 때 날짜와 시간을 나열하고 목록을 UITableView 내에 배치해야하는 응용 프로그램을 만듭니다. 내 생각은 사용자가 버튼을 누를 때마다 날짜와 타임 스탬프를 가져온 다음 버튼을 클릭 할 때마다 날짜와 시간의 사전 객체 배열에 저장하는 것입니다. 또한 단추 클릭 기록에 대한 목록과 함께 UITableView를 표시하는 모달보기를로드하는 다른 단추도 있습니다.단추 누르기 기록을 사용하여 UITableView를 만드는 방법

배열 내부에 사전 항목이 채워지는 식으로 테이블을 부분적으로 처리 할 수있었습니다. 문제는 항상 모든 항목에 대해 동일한 시간과 날짜로 끝납니다.

다음은 초기에 항목이 하나있는 표의 스크린 샷입니다. enter image description here

그리고이 버튼을 여러 번 두드리면 어떻게됩니까? 모든 행의 갱신 된 시간을 표시합니다. enter image description here

기록을 테이블에 표시하고 업데이트하지 못하게하려면 어떻게해야합니까? NSUserDefaults를 단순히 데이터 저장에도 사용하고 있습니다.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CustomCell *customCell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomCell"]; 
    if (customCell == nil) 
    { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" 
                owner:self 
                options:nil]; 
     for (id oneObject in nib) if ([oneObject isKindOfClass:[CustomCell class]]) 
      customCell = (CustomCell *)oneObject; 
    } 

    customCell.dateLbl.text = [tableDict objectForKey:@"Date"]; 
    customCell.timeLbl.text = [tableDict objectForKey:@"Time"]; 

    return customCell; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 74; 
} 

답변

0

가 좋아 내가 찾은 것 같아요 : 내 테이블 방법입니다

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    NSString *time = [defaults objectForKey:kTimeStampText]; 
    NSString *date = [defaults objectForKey:kDateText]; 

    tableDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:time, @"Time", date, @"Date", nil]; 

    tableArray = [[NSMutableArray alloc] initWithObjects:tableDict, nil]; 
} 

: 이것은 내있는 viewDidLoad입니다

- (IBAction)btnClicked:(id)sender 
{ 
    NSLog(@"Button pressed"); 

    // Gets the current time and formats it 
    NSDate *timeNow = [NSDate date]; 
    NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init]; 
    [timeFormatter setDateFormat:@"HH:mm a"]; 

    // Gets the current date and formats it 
    NSDate *dateNow = [[NSDate alloc] init]; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"MMM dd, yyyy"]; 

    NSString *currentTime = [timeFormatter stringFromDate:timeNow]; 
    NSString *currentDate = [dateFormatter stringFromDate:dateNow]; 
    NSString *timestamp = currentTime; 
    NSString *date = currentDate; 

    // This is where values gets saved 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    [defaults setObject:timestamp  forKey:@"TimeStamp"]; 
    [defaults setObject:date   forKey:@"Date"]; 
    [defaults synchronize]; 

    NSString *time = [defaults objectForKey:@"TimeStamp"]; 
    NSString *dateToday = [defaults objectForKey:@"Date"]; 

    tableVC.tableDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:time, @"Time", dateToday, @"Date", nil]; 

    [tableVC.tableArray addObject:tableVC.tableDict]; 
    [tableVC.table reloadData]; 
} 

: 여기

내 버튼을 클릭 함 방법의 일부 코드이다 귀하의 문제, 당신은 각 테이블 행에 대해 동일한 날짜에 액세스하고 있습니다.

customCell.dateLbl.text = [tableDict objectForKey:@"Date"]; 
customCell.timeLbl.text = [tableDict objectForKey:@"Time"]; 

NSDictionarry *dict = [tableArray objectAtIndex:[indexPath row]]; 
customCell.dateLbl.text = [dict objectForKey:@"Date"]; 
customCell.timeLbl.text = [dict objectForKey:@"Time"]; 

이제 정상적으로 잘 그것을 통해 루프 및 인쇄 모든 날짜 올바르지 않습니다.

코드에 문제가있는 것은 tableview의 각 행에 대해 인덱스 경로 메서드의 행이 호출된다는 것입니다. 즉 루프와 같아서 각 셀 속성을 자체적으로 할당하므로 의미가 있습니다.

+0

대신이 코드를 사용했고 작동했습니다. NSDictionary * dict = [tableArray objectAtIndex : [indexPath row]]; customCell.dateLbl.text = [dict objectForKey : @ "날짜"]; customCell.timeLbl.text = [dict objectForKey : @ "시간"]; 감사! – jaytrixz

관련 문제