2011-11-22 62 views
1

我有一個具有兩個UI組件的UIViewController:UITableView和UIDatePicker。我的目標是創建一個像ical-> add-> Start的屏幕。來自UIDatePicker的UITableViewCell中的實時值

我添加了tableView委託和dateSource到我的頭文件。在實現文件中,我想顯示我的tableCell中的datePicker的當前值(如iCal開始&結束屏幕)。

我的.m如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    [datePicker addTarget:self action:@selector(datePickerValueChanged) forControlEvents:UIControlEventValueChanged]; 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section]; 
    NSMutableArray *dict = [dictionary objectForKey:@"DATA"]; 


    if(indexPath.row == 1){ 

     UISwitch *switchSpecifyEnd = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease]; 
     cell.accessoryView = switchSpecifyEnd; 
     [(UISwitch *)cell.accessoryView setOn:NO]; // Or NO, obviously! 
     [(UISwitch *)cell.accessoryView addTarget:self action:@selector(switchSpecifyEnd) 
           forControlEvents:UIControlEventValueChanged]; 

     cell.textLabel.text = [dict objectAtIndex:indexPath.row]; 

    }else if(indexPath.row == 3){ 

     UISwitch *switchOpenEnd = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease]; 
     cell.accessoryView = switchOpenEnd; 
     [(UISwitch *)cell.accessoryView setOn:NO]; // Or NO, obviously! 
     [(UISwitch *)cell.accessoryView addTarget:self action:@selector(switchOpenEnd) 
           forControlEvents:UIControlEventValueChanged]; 
     cell.textLabel.text = [dict objectAtIndex:indexPath.row]; 

    }else{ 

     cell.textLabel.text = [dict objectAtIndex:indexPath.row]; 
//  NSLog(@"Update Cell: %@",<but how>); 

    } 

    return cell; 

} 

- (void)datePickerValueChanged{ 
    NSLog(@"Logging: %@", [NSString stringWithFormat:@"%@",datePicker.date]); 
} 

我怎樣才能從日期選擇器的值更新我的TableCell?

BR, mybecks

回答

2

要更新需要重新載入你的表,當你的桌子會重新再會的cellForRowAtIndexPath get調用,你可以更新你的文字中的表格單元格。

想這是你的方法,當你在日期選擇器選擇

-(IBAction)setStartTimeMethod 
{ 
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
    [dateFormat setDateFormat:@"EEE MMM dd, yyyy hh:mm a"]; 
    self.startTimeString = [dateFormat stringFromDate:[timeStartPicker date]]; 
    [dateFormat release]; 
    [optionsTableView reloadData]; 
    } 
+0

奏效一些日期被調用。非常感謝。 – mybecks